编辑: 这是完整的代码。修改了代码以使用路由器,但用例是相同的。一旦我使用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;
}
答案 0 :(得分:0)
为了遵守规则,我创建了一个包含我的问题的新线程。应该删除这个答案。
感谢。
泽埃夫