我创建了一个会话和一个频道。请求(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")显示,通道不是第一次发出,而是第二次发出。
发布于 2016-10-26 19:29:06
是。非交互式exec是为了执行单个命令,因此只能在单个通道上运行,在正式文件中是可见的。
https://stackoverflow.com/questions/38592865
复制相似问题