【问题标题】:libssh2: How to send data through libssh2_channel_writelibssh2:如何通过 libssh2_channel_write 发送数据
【发布时间】:2016-08-20 06:23:44
【问题描述】:

编辑: 这是完整的代码。修改了代码以与路由器一起使用,但用例是相同的。一旦我使用 libssh2_channel_write() 发出密码,随后的 libssh2_channel_read() 将因 LIBSSH2_ERROR_SOCKET_RECV 而失败。不知道为什么。我无法将后续命令发送到远程设备并获取它们的输出。

逻辑是在远程设备( libssh2_channel_exec )上执行命令。此命令执行将引发客户端输入的密码。现在通过 libssh2_channel_read() 读取流并确保正在询问密码并通过 libssh2_channel_write() 将密码写入通道。通过执行后续读取 [ THIS WHERE THE LIB IS FAILING WITH ERROR_SOCKET_RECV ] 确保远程设备上接受密码,然后通过 libssh2_channel_write() 发送要执行的命令并读取命令输出。我错过了什么吗?

    for( ;; )
    {
        /* loop until we block */ 
        int rc;
        do
        {
            char buffer[0x4000];
            rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );

            if( rc > 0 )
            {
                int i;
                char *enable = "check-password\n";
                int ret;
                bytecount += rc;
                fprintf(stderr, "We read [%d] bytes:\n", bytecount);
                fputc('[', stderr);
                for( i=0; i < rc; ++i )
                    fputc( buffer[i], stderr);
                fputc(']', stderr);


                if ( strstr(buffer, "Password:") != NULL ){
                    fprintf(stderr, "Sending the password now\n");
                    while((ret = libssh2_channel_write(channel, enable, strlen(enable))) == LIBSSH2_ERROR_EAGAIN) {
                        printf("ERROR_EAGAIN - sending password again\n");
                    }

                    fprintf(stderr, "Wrote [%d] bytes: \n", ret);
                    flag = 1;
                    continue;
                }
                if (!flag){ // start
                    char *cmd = "show clock\n";
                    int ret;
                    fprintf(stderr, "THIS Fetching show clock command now\n");
                    while((ret = libssh2_channel_write(channel, cmd, strlen(cmd))) == LIBSSH2_ERROR_EAGAIN) {
                        printf("ERROR_EAGAIN - sending show clock again\n");
                    }
                    flag = 1;
                } // end
            }
            else {
                if(rc != LIBSSH2_ERROR_EAGAIN)
                    fprintf(stderr, "libssh2_channel_read returned [%d]:\n ", rc);
            }
        }
        while( rc > 0 );

        /* this is due to blocking that would occur otherwise so we loop on
           this condition */ 
        if( rc == LIBSSH2_ERROR_EAGAIN )
        {
            int check;
            check = waitsocket(sock, session);
        }
        else
            break;
    }

【问题讨论】:

  • 如果您使用的是 libssh2 :您需要发布相应的代码...
  • 感谢您查看代码。我已经删除了一个详细的说明。
  • 不,这是 StackOverFlow 规则的一部分:如果您要解决问题,则必须在代码中包含 minimal 部分。
  • 我是这个列表的新手,我知道这一点,抱歉。我已经编辑了代码以包含有问题的部分。一旦我执行 libssh2_channel_write() 来发送密码,随后的 libssh2_channel_read() 将失败并显示 LIBSSH2_ERROR_SOCKET_RECV。想法是发送密码,通过读取通道确保密码正确(如果密码正确,将得到设备提示),然后发送命令在设备上执行并进行后续读取以获取设备输出。我错过了什么吗?

标签: passwords libssh2


【解决方案1】:

为了遵守规则,我创建了一个包含我的问题的新线程。这个答案应该被删除。

谢谢。

泽夫

【讨论】:

  • 这不是答案是评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
  • 2016-04-11
  • 1970-01-01
相关资源
最近更新 更多