Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int SSH2::ExecCmd(std::string sCmd, std::vector<std::string> &vResults, int &nReturnCode)
- {
- int rc;
- int bytecount = 0;
- char *exitsignal=(char *)"none";
- char buffer[STDOUT_BUF_SIZE];
- while( (rc = libssh2_channel_exec(m_pChannel, sCmd.c_str())) ==
- LIBSSH2_ERROR_EAGAIN )
- {
- waitsocket();
- }
- if( rc != 0 )
- {
- printf("Remote command execution failed\n");
- libssh2_session_disconnect(m_pSession, "Disconnecting");
- libssh2_session_free(m_pSession);
- Sleep(1000);
- closesocket(m_nSock);
- return FAIL;
- }
- for( ;; )
- {
- /* loop until we block */
- int rc;
- do
- {
- memset(buffer, '\0', STDOUT_BUF_SIZE);
- rc = libssh2_channel_read( m_pChannel, buffer, STDOUT_BUF_SIZE );
- if( rc > 0 )
- {
- int i;
- bytecount += rc;
- string sBuf = buffer;
- //std::cout << sBuf;
- vResults.push_back(sBuf);
- }
- else {
- 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();
- }
- else
- break;
- }
- /* while( (rc = libssh2_channel_close(m_pChannel)) == LIBSSH2_ERROR_EAGAIN )
- waitsocket();*/
- nReturnCode = 127;
- if( rc == 0 )
- {
- nReturnCode = libssh2_channel_get_exit_status( m_pChannel );
- libssh2_channel_get_exit_signal(m_pChannel, &exitsignal,
- NULL, NULL, NULL, NULL, NULL);
- }
- if (exitsignal)
- printf("\nGot signal: %s\n", exitsignal);
- else
- printf("\nEXIT: %d bytecount: %d\n", nReturnCode, bytecount);
- return SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement