libssh隧道回显输入

时间:2019-01-22 13:54:35

标签: c linux libssh

通过遵循http://api.libssh.org/master/libssh_tutorial.html上的教程并将其他示例中的内容拼接在一起,我使用libssh创建了自己的SSH反向隧道。但是,现在,我得到的只是客户端回显通过隧道连接输入的任何内容。我试图达到可以通过反向隧道执行命令的位置(例如:function triggerViewPhone(container) { var trigger = container.viewPhone == false || container.viewPhone == undefined ? true : false; this.$set(container, 'viewPhone', trigger); } function triggerAddUser(container) { var trigger = container.addUser == false || container.addUser == undefined ? true : false; this.$set(container, 'addUser', trigger); } )。

反向隧道(在客户端启动):

ls -al

运行此命令,将启动反向会话。因此,可以从SSH服务器运行:

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;
}

然后在客户端,我看到以下输出:

> 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的实际结果(或用户输入的任何系统命令,而不是回声。任何人都可以引导我完成我错过的步骤)这样吗?

谢谢!

0 个答案:

没有答案