我尝试使用libssh2通过ssh发送命令,但无法接收预期的响应。
它与我的覆盆子pi很好地连接。
发送命令的功能:
char * s7c_hardware::sendCmd(char * cmd, bool out)
{
if (!(channel = libssh2_channel_open_session(session))) return "-1";
libssh2_channel_setenv(channel, "FOO", "bar");
if (libssh2_channel_request_pty(channel, "vanilla")) return "-2";
if (libssh2_channel_shell(channel)) return "-3";
int rc;
while ((rc = libssh2_channel_exec(channel, cmd)) ==
LIBSSH2_ERROR_EAGAIN)
{
waitsocket(sock, session);
}
char * output = "";
if (out)
{
do
{
char buffer[99999];
rc = libssh2_channel_read(channel, buffer, sizeof(buffer));
if (rc > 0)
{
fprintf(stderr, "We read:\n");
for (int i = 0; i < rc; ++i)
fputc(buffer[i], stderr);
fprintf(stderr, "\n");
}
else {
if (rc != LIBSSH2_ERROR_EAGAIN)
fprintf(stderr, "libssh2_channel_read returned %d\n", rc);
}
} while (rc > 0);
}
libssh2_channel_free(channel);
channel = NULL;
return output;
}
cmd
是命令,out
是否想要输出。
如果我尝试发送&#34;正常运行时间&#34;收到上来的时间,我收到了这个:
We read:
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Jul 1 10:27:51 2017 from desktop-ca160hm.local
We read:
pi@rasp:~$
答案 0 :(得分:0)
我认为请求shell并请求单个命令的执行是互斥的。如果要运行命令,获取其输出并结束通道(不一定是会话),则应运行libssh2_channel_exec
而不运行libssh2_channel_shell
。