#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();