编辑:这是完整的代码。修改了代码以使用路由器,但用例是相同的。一旦我使用libssh2_channel_write()发出密码,随后的libssh2_channel_read()就会失败,并返回LIBSSH2_ERROR_SOCKET_RECV。不知道为什么。我无法向远程设备发送后续命令并获取其输出。
逻辑是在远程设备( libssh2_channel_exec )上执行命令。此命令的执行将抛出一个密码,供客户端输入。现在通过libssh2_channel_read()读取流,并确保正在询问密码,并通过libssh2_channel_write()将密码写入通道。通过执行后续读取,确保密码在远程设备上被接受。这就是LIB使用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;
}发布于 2016-08-20 05:26:03
为了遵循规则,我创建了一个包含我的问题的新帖子。此答案应删除。
谢谢。
Zeev
https://stackoverflow.com/questions/23024170
复制相似问题