|
利用ssh2向下位机发送两条命令,
命令为:
echo Admin | sudo – S ip addr add 192.168.1.104/24 dev FE1
sudo sh runsh.sh
libssh2_channel_exec 返回值为0 ,但是不执行,想请问一下大佬有什么办法?
CString tempstr;
char *hostname = NULL;
char *username = NULL;
char *password = NULL;
char *cmdch1 = NULL;
char *cmdch2 = NULL;
CString_to_char(CSFtpServerConfig::GetServerIP(),&hostname );
CString_to_char(CSFtpServerConfig::GetSftpUser(), &username);
CString_to_char(CSFtpServerConfig::GetSftpPsw(), &password);
CString_to_char(cmdstr1,&cmdch1);
CString_to_char(cmdstr2,&cmdch2);
long port = CSFtpServerConfig::GetSftpPort() ; // 获取 端口号,IP地址,用户名和密码
unsigned long hostaddr;
int sock;
struct sockaddr_in sin;
const char *fingerprint;
LIBSSH2_SESSION *session2;
LIBSSH2_CHANNEL *channel;
int rc;
int exitcode;
char *exitsignal=(char *)"none";
int bytecount = 0;
size_t len;
LIBSSH2_KNOWNHOSTS *nh;
int type;
#ifdef WIN32
WSADATA wsadata;
WSAStartup(MAKEWORD(2,0), &wsadata);
#endif
rc = libssh2_init (0);
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
return 1;
}
hostaddr = inet_addr(hostname);
/* Ultra basic "connect to port 22 on localhost"
* Your code is responsible for creating the socket establishing the
* connection
*/
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return -1;
}
tempstr = "socket建立成功";
Global_WriteRecordFile(tempstr);
/* Create a session instance */
session2 = libssh2_session_init();
if (!session2)
return -1;
/* tell libssh2 we want it all done non-blocking */
libssh2_session_set_blocking(session2, 0);
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
while ((rc = libssh2_session_handshake(session2, sock)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
fprintf(stderr, "Failure establishing SSH session2: %d\n", rc);
return -1;
}
nh = libssh2_knownhost_init(session2);
if(!nh) {
/* eeek, do cleanup here */
return 2;
}
/* read all hosts from here */
libssh2_knownhost_readfile(nh, "known_hosts",
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
/* store all known hosts to here */
libssh2_knownhost_writefile(nh, "dumpfile",
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
fingerprint = libssh2_session_hostkey(session2, &len, &type);
if(fingerprint) {
struct libssh2_knownhost *host;
#if LIBSSH2_VERSION_NUM >= 0x010206
/* introduced in 1.2.6 */
int check = libssh2_knownhost_checkp(nh, hostname, port,
fingerprint, len,
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
LIBSSH2_KNOWNHOST_KEYENC_RAW,
&host);
#else
/* 1.2.5 or older */
int check = libssh2_knownhost_check(nh, hostname,
fingerprint, len,
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
LIBSSH2_KNOWNHOST_KEYENC_RAW,
&host);
#endif
fprintf(stderr, "Host check: %d, key: %s\n", check,
(check <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH)?
host->key:"<none>");
/*****
* At this point, we could verify that 'check' tells us the key is
* fine or bail out.
*****/
}
else {
/* eeek, do cleanup here */
return 3;
}
libssh2_knownhost_free(nh);
if ( strlen(password) != 0 ) {
/* We could authenticate via password */
while ((rc = libssh2_userauth_password(session2, username, password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
fprintf(stderr, "Authentication by password failed.\n");
goto shutdown;
}
}
else {
/* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session2, username,
"/home/user/"
".ssh/id_rsa.pub",
"/home/user/"
".ssh/id_rsa",
password)) ==
LIBSSH2_ERROR_EAGAIN);
if (rc) {
fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown;
}
}
tempstr = "密码验证成功";
Global_WriteRecordFile(tempstr);
#if 0
libssh2_trace(session2, ~0 );
#endif
// 第一个命令
/* Exec non-blocking on the remove host */
while( (channel = libssh2_channel_open_session(session2)) == NULL &&
libssh2_session_last_error(session2,NULL,NULL,0) ==
LIBSSH2_ERROR_EAGAIN )
{
waitsocket(sock, session2);
}
if( channel == NULL )
{
fprintf(stderr,"Error\n");
exit( 1 );
}
Global_WriteRecordFile(_T("libssh2_channel_open_session 成功!"));
while( (rc = libssh2_channel_exec(channel, cmdch1)) ==
LIBSSH2_ERROR_EAGAIN )
{
waitsocket(sock, session2);
}
if( rc != 0 )
{
fprintf(stderr,"Error\n");
exit( 1 );
}
for( ;; )
{
/* loop until we block */
int rc;
do
{
char buffer[0x4000];
memset(buffer,0,sizeof(buffer)) ;
rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );
tempstr.Format(_T("Read返回值=%d"),rc);
Global_WriteRecordFile(tempstr);
if( rc > 0 )
{
tempstr = buffer ;
Global_WriteRecordFile(tempstr);
int i;
bytecount += rc;
fprintf(stderr, "We read:\n");
for( i=0; i < rc; ++i )
fputc( buffer[i], stderr);
fprintf(stderr, "\n");
}
else {
if( rc != LIBSSH2_ERROR_EAGAIN )
/* no need to output this for the EAGAIN case */
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 )
{
waitsocket(sock, session2);
}
else
break;
}
//********************** 第二个命令
while( (channel = libssh2_channel_open_session(session2)) == NULL &&
libssh2_session_last_error(session2,NULL,NULL,0) ==
LIBSSH2_ERROR_EAGAIN )
{
waitsocket(sock, session2);
}
if( channel == NULL )
{
fprintf(stderr,"Error\n");
exit( 1 );
}
Global_WriteRecordFile(_T("libssh2_channel_open_session 成功!"));
while( (rc = libssh2_channel_exec(channel, cmdch2)) ==
LIBSSH2_ERROR_EAGAIN )
{
waitsocket(sock, session2);
}
if( rc != 0 )
{
fprintf(stderr,"Error\n");
exit( 1 );
}
for( ;; )
{
/* loop until we block */
int rc;
do
{
char buffer[0x4000];
memset(buffer,0,sizeof(buffer)) ;
rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );
tempstr.Format(_T("Read返回值=%d"),rc);
Global_WriteRecordFile(tempstr);
if( rc > 0 )
{
tempstr = buffer ;
Global_WriteRecordFile(tempstr);
int i;
bytecount += rc;
fprintf(stderr, "We read:\n");
for( i=0; i < rc; ++i )
fputc( buffer[i], stderr);
fprintf(stderr, "\n");
}
else {
if( rc != LIBSSH2_ERROR_EAGAIN )
/* no need to output this for the EAGAIN case */
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 )
{
waitsocket(sock, session2);
}
else
break;
}
//*********************
exitcode = 127;
while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN )
waitsocket(sock, session2);
if( rc == 0 )
{
exitcode = libssh2_channel_get_exit_status( channel );
libssh2_channel_get_exit_signal(channel, &exitsignal,
NULL, NULL, NULL, NULL, NULL);
}
if (exitsignal)
fprintf(stderr, "\nGot signal: %s\n", exitsignal);
else
fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
libssh2_channel_free(channel);
channel = NULL;
shutdown:
libssh2_session_disconnect(session2,
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session2);
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
fprintf(stderr, "all done\n");
libssh2_exit();
delete hostname ;
delete username ;
delete password ;
delete cmdch1 ;
delete cmdch2 ;
return 0; |
上一篇: 我想解析一个软键盘(finalSKB)的原理,但是我太渣了实在分析不动下一篇: Graphics.h
|