libssh-多次发送请求并从通道读取应答
libssh - send request multiple times and read answer from channel
我创建了一个会话和一个通道。大约每秒都会发送一个请求(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:"检查远程是否发送了EOF")显示通道不是第一次,而是第二次。
是。非交互式exec用于执行单个命令,因此您只能在单个通道中运行,如官方文档中所示。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 从Qt中的每个像素读取Alpha通道
- libssh-多次发送请求并从通道读取应答