SSH隧道与c库

时间:2018-04-17 15:21:28

标签: c ssh portforwarding tunnel libssh

我正在尝试使用libssh库来打开SSH隧道。

基本上,我需要通过隧道到达服务器。首先,我需要SSH到网关(我已经这样做了),然后我需要从该主机SSH到服务器:

[PC] - ssh www@server - > [网关] - ls -all - > [服务器]

第一个ssh需要密码才能到达网关,但第二个ssh需要不同的用户,并且不需要密码。

我想要访问该服务器并执行脚本,在这种情况下,它只是一个命令:ssh_channel_open_forward()

我尝试使用int direct_forwarding(ssh_session session) { ssh_channel channel; int rc = -1; int nbytes, nwritten; channel = ssh_channel_new(session); if (channel == NULL) { return rc; } printf("\n*********channel is created************\n"); rc = ssh_channel_open_forward(channel, "server", 22, "gateway", 22); if (rc != SSH_OK) { ssh_channel_free(channel); return rc; } printf("\n*********channel is opened************\n"); char *command = "ls -all"; nbytes = strlen(command); nwritten = ssh_channel_write(channel, command, nbytes); if (nbytes != nwritten) { ssh_channel_free(channel); return SSH_ERROR; } printf("\n*********Write command is finished************\n"); char buffer[256]; nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); while (nbytes > 0) { printf("number of bytes read : %d\n", nbytes); if (write(1, buffer, nbytes) != (unsigned int)nbytes) { ssh_channel_close(channel); ssh_channel_free(channel); return SSH_ERROR; } nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); } if (nbytes < 0) { ssh_channel_close(channel); ssh_channel_free(channel); return SSH_ERROR; } ssh_channel_send_eof(channel); ssh_channel_close(channel); printf("\n*******************no errors*******************\n"); ssh_channel_free(channel); return SSH_OK; } ,但我不知道在哪里指定'用户'加上它在我使用时挂起。没有太多的例子或大量的文档。

以下是我尝试通过网桥/隧道访问服务器的方法:

{{1}}

这可能在libssh吗?

0 个答案:

没有答案