赞
踩
在 Linux 上,SSH 是一种非常常用的远程登录协议。除了使用 ssh 命令行工具之外,我们还可以在 C++ 程序中使用 SSH 客户端库来实现远程登录和文件传输等功能。本文就来介绍两个常用的 C++ SSH 客户端库:libssh 和 libssh2。
libssh 是一个轻量级的 SSH 客户端库,可用于在 C++ 程序中实现 SSH 客户端功能。它提供了简单易用的 API,可以用来执行远程命令、传输文件以及在通道上进行加密通信等。以下是一个使用 libssh 连接远程服务器并执行远程命令的示例代码:
#include <stdio.h> #include <libssh/libssh.h> int main(int argc, char** argv) { ssh_session session; int rc; session = ssh_new(); if (session == NULL) exit(-1); ssh_options_set(session, SSH_OPTIONS_HOST, "remote_host"); ssh_options_set(session, SSH_OPTIONS_USER, "remote_username"); rc = ssh_connect(session); if (rc != SSH_OK) { fprintf(stderr, "Error connecting to remote host: %s\n", ssh_get_error(session)); ssh_free(session); exit(-1); } rc = ssh_userauth_password(session, NULL, "remote_password"); if (rc != SSH_AUTH_SUCCESS) { fprintf(stderr, "Authentication failed: %s\n", ssh_get_error(session)); ssh_disconnect(session); ssh_free(session); exit(-1); } ssh_channel channel = ssh_channel_new(session); if (channel == NULL) { fprintf(stderr, "Error creating SSH channel: %s\n", ssh_get_error(session)); ssh_disconnect(session); ssh_free(session); exit(-1); } rc = ssh_channel_open_session(channel); if (rc != SSH_OK) { fprintf(stderr, "Error opening SSH channel: %s\n", ssh_get_error(session)); ssh_channel_free(channel); ssh_disconnect(session); ssh_free(session); exit(-1); } rc = ssh_channel_request_exec(channel, "ls -al"); if (rc != SSH_OK) { fprintf(stderr, "Error executing remote command: %s\n", ssh_get_error(session)); ssh_channel_close(channel); ssh_channel_free(channel); ssh_disconnect(session); ssh_free(session); exit(-1); } char buffer[256]; int nbytes; while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) { printf("%.*s", nbytes, buffer); } if (nbytes < 0) { fprintf(stderr, "Error reading SSH channel: %s\n", ssh_get_error(session)); ssh_channel_close(channel); ssh_channel_free(channel); ssh_disconnect(session); ssh_free(session); exit(-1); } ssh_channel_send_eof(channel); ssh_channel_close(channel); ssh_channel_free(channel); ssh_disconnect(session); ssh_free(session); return 0; }
在这个示例代码中,我们使用 ssh_new()
函数创建一个新的 SSH 会话。然后,我们使用 ssh_options_set()
函数设置远程主机地址和用户名,并使用 ssh_connect()
函数连接到远程主机。
接下来,我们使用 ssh_userauth_password()
函数进行密码认证,并检查认证是否成功。如果成功,我们就使用 ssh_channel_new()
函数创建一个新的 SSH 通道,并使用 ssh_channel_open_session()
函数打开通道。
然后,我们可以使用 ssh_channel_request_exec()
函数在远程服务器上执行命令(例如 “ls -al”),并使用 ssh_channel_read()
函数读取命令输出。最后,我们关闭通道并释放 SSH 会话。
libssh2 是另一个常用的 SSH 客户端库,它提供了更高级的功能,例如多进程/线程支持、SFTP 文件传输等。以下是一个使用 libssh2 连接远程服务器并传输文件的示例代码:
#include <stdio.h> #include <libssh2.h> int main(int argc, char** argv) { int rc, sock; rc = libssh2_init(0); if (rc != 0) exit(-1); sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) exit(-1); struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = inet_addr("remote_host"); rc = connect(sock, (struct sockaddr*)&sin, sizeof(sin)); if (rc != 0) exit(-1); LIBSSH2_SESSION* session; session = libssh2_session_init(); if (session == NULL) exit(-1); libssh2_session_set_blocking(session, 1); rc = libssh2_session_handshake(session, sock); if (rc != 0) exit(-1); rc = libssh2_userauth_password(session, "remote_username", "remote_password"); if (rc != 0) exit(-1); LIBSSH2_SFTP* sftp_session; sftp_session = libssh2_sftp_init(session); if (sftp_session == NULL) exit(-1); LIBSSH2_SFTP_HANDLE* sftp_handle; char buf[1024]; sprintf(buf, "/path/to/local/file"); sftp_handle = libssh2_sftp_open(sftp_session, buf, LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); if (sftp_handle == NULL) exit(-1); sprintf(buf, "/path/to/remote/file"); FILE* file = fopen(buf, "rb"); if (file == NULL) exit(-1); int bytes; do { bytes = fread(buf, 1, sizeof(buf), file); if (bytes > 0) { int rc = libssh2_sftp_write(sftp_handle, buf, bytes); if (rc != bytes) exit(-1); } } while (bytes > 0); libssh2_sftp_close(sftp_handle); libssh2_sftp_shutdown(sftp_session); libssh2_session_disconnect(session, "Bye"); libssh2_session_free(session); libssh2_exit(); return 0; }
在这个示例代码中,我们使用 libssh2_init()
函数初始化 libssh2 库,并使用 socket()
函数创建一个 TCP/IP 套接字并连接到远程主机。
然后,我们使用 libssh2_session_init()
函数创建一个新的 SSH 会话,并使用 libssh2_session_handshake()
函数与远程主机建立安全连接。接下来,我们使用 libssh2_userauth_password()
函数进行密码认证。
在认证成功之后,我们使用 libssh2_sftp_init()
函数初始化 SFTP 会话,并使用 libssh2_sftp_open()
函数打开远程文件以进行写入操作。然后,我们使用标准 C 文件操作函数 fopen()
和 fread()
读取本地文件的内容,并使用 libssh2_sftp_write()
函数将数据写入远程文件中。
最后,我们关闭 SFTP 会话和 SSH 会话,并释放相应的资源。
总结来说,如果你需要在 C++ 程序中实现 SSH 客户端功能,并且只需要基本的远程命令执行和文件传输功能,那么你可以使用 libssh 库。如果你需要更高级的功能,例如多进程/线程支持和 SFTP 文件传输等,则可以考虑使用 libssh2 库。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。