libssh2--ssh2实例

#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;
    }
View Code

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

 

转载于:https://www.cnblogs.com/jingliang10101/p/9816301.html

在Qt中使用libssh2实现SSH客户端可以分为以下步骤: 1. 在Qt项目中引入libssh2库,可以将libssh2的头文件和库文件手动复制到项目目录中,也可以通过在.pro文件中添加LIBS和INCLUDEPATH来引入库文件和头文件,例如: ``` INCLUDEPATH += /path/to/libssh2/include LIBS += -L/path/to/libssh2/lib -lssh2 ``` 2. 创建SSH会话,连接到远程主机,可以使用以下代码: ``` #include <libssh2.h> // 创建SSH会话 LIBSSH2_SESSION *session = libssh2_session_init(); // 连接到远程主机 int rc = libssh2_session_startup(session, socket); if (rc) { // 连接失败 libssh2_session_free(session); return; } ``` 其中,socket是一个已连接到远程主机的套接字。 3. 认证用户身份,可以使用密码或者公钥进行认证,例如: 使用密码认证: ``` // 使用用户名和密码进行认证 rc = libssh2_userauth_password(session, "username", "password"); if (rc) { // 认证失败 libssh2_session_disconnect(session, "Failed to authenticate"); libssh2_session_free(session); return; } ``` 使用公钥认证: ``` // 加载私钥 rc = libssh2_userauth_publickey_fromfile(session, "username", "/path/to/private_key", "/path/to/public_key", "password"); if (rc) { // 认证失败 libssh2_session_disconnect(session, "Failed to authenticate"); libssh2_session_free(session); return; } ``` 4. 执行远程命令或者传输文件,可以使用libssh2_channel_exec和libssh2_scp_send等函数来执行远程命令或者传输文件,例如: 执行远程命令并获取输出: ``` // 打开一个shell通道 LIBSSH2_CHANNEL *channel = libssh2_channel_open_session(session); // 执行远程命令 libssh2_channel_exec(channel, "ls -l"); // 读取输出 char buffer[1024]; int nbytes; while ((nbytes = libssh2_channel_read(channel, buffer, sizeof(buffer))) > 0) { // 处理输出 } // 关闭通道 libssh2_channel_close(channel); libssh2_channel_free(channel); ``` 传输文件: ``` // 打开SCP会话 LIBSSH2_SCP *scp = libssh2_scp_send(session, "/path/to/remote/file", 0644, filesize); // 传输文件 char buffer[1024]; int nbytes; while ((nbytes = fread(buffer, 1, sizeof(buffer), file)) > 0) { libssh2_scp_send_data(scp, buffer, nbytes); } // 关闭SCP会话 libssh2_scp_send_eof(scp); libssh2_scp_recv_flush(scp); libssh2_scp_close(scp); libssh2_scp_free(scp); ``` 以上就是使用libssh2实现SSH客户端的基本步骤,需要根据具体的需求进行调整和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值