http://api.libssh.org/master/libssh_tutorial.html
把其他样品拼在一起。然而,现在,我得到的只是客户机回显通过隧道连接输入的任何内容。我正在尝试通过反向隧道执行命令(例如:
ls -al
).
反向隧道(在客户端启动):
int reverse_loop(ssh_session session){
ssh_channel channel;
int rc;
int nbytes, nwritten;
char buf[256];
int port = 0;
rc = ssh_channel_listen_forward(session, NULL, 43434, NULL);
if (rc != SSH_OK){
fprintf(stderr, "Error opening remote port %s\n", ssh_get_error(session));
return rc;
}
channel = ssh_channel_accept_forward(session, 60000, &port);
if (channel == NULL){
fprintf(stderr, "Error waiting for incoming connection: %s\n", ssh_get_error(session));
return SSH_ERROR;
}
while(1){
printf("In loop\n");
nbytes = ssh_channel_read(channel, buf, sizeof(buf), 0);
if (nbytes < 0){
fprintf(stderr, "Error reading incoming data: %s\n", ssh_get_error(session));
ssh_channel_send_eof(channel);
ssh_channel_free(channel);
return SSH_ERROR;
}
printf("read channel\n");
if (nbytes > 0){
nwritten = ssh_channel_write(channel, buf, nbytes);
if (nwritten != nbytes){
fprintf(stderr, "Error sending answer: %s\n", ssh_get_error(session));
ssh_channel_send_eof(channel);
ssh_channel_free(channel);
return SSH_ERROR;
}
printf("Wrote channel\n");
}
printf("sent answer!\n");
}
// close_channel
ssh_channel_send_eof(channel);
ssh_channel_free(channel);
return SSH_OK;
}
运行此命令后,将启动反向会话。因此,在SSH服务器上,我可以运行:
> nc localhost 43434
ls (this is what I sent)
ls (this is what I receive)
pwd (this is what I sent)
pwd (this is what I receive)
In loop
read channel
Wrote channel
sent answer!
In loop
我要找的是跑步的实际效果
ls
或
pwd
谢谢!