#include "libssh2_config.h"
#include<libssh2.h>
#include<libssh2_sftp.h>
上述為所包含必備頭文件。
以下為定義的靜態子串常量
const char *keyfile1 = "~/.ssh/id_rsa.pub"; const char *keyfile2 = "~/.ssh/id_rsa"; const char *username = "username"; const char *password = "password";
unsigned long hostaddr; int rc, sock, i, auth_pw = 0; struct sockaddr_in_sin; const char *fingerprint; char * userauthlist; LIBSSH2_SESSION *session; LIBSSH2_CHANNEL *channel;
連接到SSH2步驟:
(1)建立socket並連接到遠程主機SSH2服務(22端口);
(2)創建一個LIBSSH2_SESSION 實例並啟動它。啟動動作包括設置歡迎橫幅、交換密鑰並且設置加密、壓縮和MAC層。
session = libssh2_session_init(); //創建一個會話實例 if(libssh2_session_handshake(session, sock)) { fprintf(stderr, "Failure establishing SSH session"); return -1; }
(3)認證:檢查主機密鑰指紋並檢查可用的認證方式。
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); userauthlist = libssh2_userauth_list(session, username, strlen(username)); if(strstr(userauthlist, "password") != NULL) { auth_pw |= 1; } if(strstr(userauthlist, "keyboad-interactive") != NULL) { auth_pw |= 2; } if(strstr(userauthlist, "publickey") != NULL) { auth_pw |= 4; }
(4)如果在參數列表中設置了認證方式,則將認證方式設為命令中的方式(前提是該方式是通過上個步驟檢測可用的)。
if(argc > 4) { if((auth_pw & 1) && !strcasecmp(argv[4], "-p")) { auth_pw = 1; } if((auth_pw & 2) && !strcasecmp(argv[4], "-i")) { auth_pw = 2; } if((auth_pw && 4) && !strcasecmp(argv[4], "-k")) { auth_pw = 4; } }
(5)根據上一步選定的認證方式開始認證。

if (auth_pw & 1) { /* We could authenticate via password */ if (libssh2_userauth_password(session, username, password)) { fprintf(stderr, "\tAuthentication by password failed!\n"); goto shutdown; } else { fprintf(stderr, "\tAuthentication by password succeeded.\n"); } } else if (auth_pw & 2) { /* Or via keyboard-interactive */ if (libssh2_userauth_keyboard_interactive(session, username, &kbd_callback) ) { fprintf(stderr, "\tAuthentication by keyboard-interactive failed!\n"); goto shutdown; } else { fprintf(stderr, "\tAuthentication by keyboard-interactive succeeded.\n"); } } else if (auth_pw & 4) { /* Or by public key */ if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; } else { fprintf(stderr, "\tAuthentication by public key succeeded.\n"); } } else { fprintf(stderr, "No supported authentication methods found!\n"); goto shutdown; }
(6)請求一個shell
if(!(channel = libssh2_channel_open_session(session)))
(7)設置一些環境變量,並上傳給服務器
libssh2_channel_setenv(channel, "F00", "bar");
(8)請求一個vanilla的終端模擬。
libssh2_channel_request_pty(channel, "vanilla")
(9)在上一步請求的pty上開啟SHELL。
libssh2_channel_shell(channel)
(10)至此,可以交互使用shell了
libssh2_channel_read(); libssh2_channel_read_stderr(); libssh2_channel_write(); libssh2_channel_write_stderr(); /* 打開或關閉阻塞模式 */ libssh2_channel_set_blocking(); /* 如果服務器發送EOF */ libssh2_channel_eof()返回非0; /* 關閉channel */ libssh2_channel_close(); /* 釋放一個channel */ libssh2_channel_free();
(11)ssh交互完成后,關閉會話並釋放會話
libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session);
(12)關閉sock並退出libssh2
close(sock);
libssh2_exit();