Advertisement
Guest User

libssh2_exec

a guest
Dec 20th, 2011
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. int SSH2::ExecCmd(std::string sCmd, std::vector<std::string> &vResults, int &nReturnCode)
  2. {
  3.     int rc;
  4.     int bytecount = 0;
  5.     char *exitsignal=(char *)"none";
  6.     char buffer[STDOUT_BUF_SIZE];
  7.  
  8.    
  9.     while( (rc = libssh2_channel_exec(m_pChannel, sCmd.c_str())) ==
  10.            LIBSSH2_ERROR_EAGAIN )
  11.     {
  12.         waitsocket();
  13.     }
  14.    
  15.     if( rc != 0 )
  16.     {
  17.         printf("Remote command execution failed\n");
  18.         libssh2_session_disconnect(m_pSession, "Disconnecting");
  19.         libssh2_session_free(m_pSession);
  20.         Sleep(1000);
  21.         closesocket(m_nSock);
  22.         return FAIL;
  23.     }
  24.  
  25.     for( ;; )
  26.     {
  27.         /* loop until we block */
  28.         int rc;
  29.         do
  30.         {
  31.            
  32.             memset(buffer, '\0', STDOUT_BUF_SIZE);
  33.             rc = libssh2_channel_read( m_pChannel, buffer, STDOUT_BUF_SIZE );
  34.             if( rc > 0 )
  35.             {
  36.                 int i;
  37.                 bytecount += rc;
  38.                
  39.                
  40.                 string sBuf = buffer;
  41.                 //std::cout << sBuf;
  42.                 vResults.push_back(sBuf);
  43.             }
  44.             else {
  45.                 fprintf(stderr, "libssh2_channel_read returned %d\n", rc);
  46.             }
  47.         }
  48.         while( rc > 0 );
  49.  
  50.         /* this is due to blocking that would occur otherwise so we loop on
  51.             this condition */
  52.         if( rc == LIBSSH2_ERROR_EAGAIN )
  53.         {
  54.             waitsocket();
  55.         }
  56.         else
  57.             break;
  58.     }
  59.    
  60.    
  61.    /* while( (rc = libssh2_channel_close(m_pChannel)) == LIBSSH2_ERROR_EAGAIN )
  62.         waitsocket();*/
  63.    
  64.     nReturnCode = 127;
  65.     if( rc == 0 )
  66.     {
  67.         nReturnCode = libssh2_channel_get_exit_status( m_pChannel );
  68.         libssh2_channel_get_exit_signal(m_pChannel, &exitsignal,
  69.                                         NULL, NULL, NULL, NULL, NULL);
  70.     }
  71.  
  72.     if (exitsignal)
  73.         printf("\nGot signal: %s\n", exitsignal);
  74.     else
  75.         printf("\nEXIT: %d bytecount: %d\n", nReturnCode, bytecount);
  76.    
  77.     return SUCCESS;
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement