C SSH远程执行命令
我正在尝试通过ssh连接到远程服务器并执行一个简单的命令。使用libssh库和以下代码C SSH远程执行命令,c,networking,ssh,packets,libssh,C,Networking,Ssh,Packets,Libssh,我正在尝试通过ssh连接到远程服务器并执行一个简单的命令。使用libssh库和以下代码 ssh_key pKey; ssh_session session; ssh_channel channel; int rc = ssh_pki_import_privkey_file(PC_PRIVATE_KEY_FILE, NULL, NULL, NULL, &pKey); char buffer[1024]; unsigned int nbytes; printf("Session...\n"
ssh_key pKey;
ssh_session session;
ssh_channel channel;
int rc = ssh_pki_import_privkey_file(PC_PRIVATE_KEY_FILE, NULL, NULL, NULL, &pKey);
char buffer[1024];
unsigned int nbytes;
printf("Session...\n");
session = ssh_new();
if (session == NULL) exit(-1);
ssh_options_set(session, SSH_OPTIONS_HOST, PC_HOST);
ssh_options_set(session, SSH_OPTIONS_USER, PC_USER);
ssh_options_set(session, SSH_OPTIONS_PORT, &PC_PORT);
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &PC_VERBOSITY);
printf("Connecting...\n");
rc = ssh_connect(session);
if (rc != SSH_OK) error(session);
printf("Channel...\n");
channel = ssh_channel_new(session);
if (channel == NULL) exit(-1);
printf("Opening...\n");
rc = ssh_channel_open_session(channel);
if (rc != SSH_OK) error(session);
printf("Executing remote command...\n");
rc = ssh_channel_request_exec(channel, "ls -l");
if (rc != SSH_OK) error(session);
printf("Received:\n");
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
while (nbytes > 0) {
fwrite(buffer, 1, nbytes, stdout);
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}
ssh connect工作成功,但当它到达创建通道并执行命令的代码部分时,我会收到以下消息:
channel_open: Creating a channel 43 with 64000 window and 32768 max packet
packet_send2: packet: wrote [len=44,padding=19,comp=24,payload=24]
channel_open: Sent a SSH_MSG_CHANNEL_OPEN type session for channel 43
ssh_socket_unbuffered_write: Enabling POLLOUT for socket
ssh_packet_socket_callback: packet: read type 3 [len=12,padding=6,comp=5,payload=5]
ssh_packet_process: Dispatching handler for packet type 3
ssh_packet_unimplemented: Received SSH_MSG_UNIMPLEMENTED (sequence number 3)
我做了一些研究,发现了一个类似于我现在讨论的问题,但被问题的首要答案弄糊涂了。还有,a,但没有解决我的问题的方法
我不知道如何解决我的问题。任何帮助都将不胜感激