【问题标题】:libssh - send request multiple times and read answer from channellibssh - 多次发送请求并从频道读取答案
【发布时间】:2016-11-30 06:40:19
【问题描述】:

我创建了一个会话和一个频道。应该大约每秒发送一个请求 (ssh_channel_request_exec),我想阅读此请求的答案 (ssh_channel_read)。但是,我找不到有关如何发出多个请求的示例,该 api 仅包含有关如何发出请求、阅读答案和关闭频道的示例。

当我尝试依次从通道请求和读取两次时,ssh_channel_request_exec 第二次返回错误。是否需要为每个请求打开一个新通道?

int ret = ssh_channel_request_exec(m_channel, request.c_str());
if (ret != SSH_OK)
{
    ssh_channel_close(m_channel);
    ssh_channel_free(m_channel);
    return false;
}

std::stringstream sstream;
nbytes = ssh_channel_read(m_channel, buffer, sizeof(buffer), 0);
while (nbytes > 0)
{
    sstream.write(buffer, nbytes);
    nbytes = ssh_channel_read(m_channel, buffer, sizeof(buffer), 0);
}

if (nbytes < 0) // 'nbytes' becomes zero the first time, the channel is not closed the second time!
{
    ssh_channel_close(m_channel);
    ssh_channel_free(m_channel);
    return false;
}

response = sstream.str();

该 api 还包含一个 ssh_channel_send_eof 函数,该函数在示例中从通道读取后使用。有什么用? api 只说 “在通道上发送文件结尾。这不会关闭通道。您仍然可以从中读取但不能写入。”

检查ssh_channel_is_eof (api: "Check if remote has sent an EOF") 表明该频道第一次不是 eof,而是第二次。

【问题讨论】:

    标签: c++ windows libssh


    【解决方案1】:

    是的。非交互式 exec 用于执行单个命令,因此您只能在单个通道上运行,如 official documentation 中所示。

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2021-09-16
      • 2021-11-17
      • 2011-05-24
      • 1970-01-01
      • 2013-10-26
      相关资源
      最近更新 更多