Note that there are some explanatory texts on larger screens.

plurals
  1. POlibssh + iPhone Implementation with multiple commands execution in sequence
    primarykey
    data
    text
    <p>It seems my questions are strange and i'm not getting enough help but I'm back. I've another strange question which needs to be solved in emergency.</p> <p>I'm developing an iPhone app. which uses <strong>libssh 2</strong> for commands execution through iPhone over remote host. It's okay and working all methods and commands if i execute them in single.</p> <p>My problem is, consider a sequence of commands,</p> <blockquote> <p><strong>pwd</strong></p> <p>=> o/p will be /Users/mac01</p> <p><strong>cd xyz</strong></p> <p>=> nothing as o/p</p> <p><strong>pwd</strong></p> <p>=> o/p will be /Users/mac01/xyz</p> </blockquote> <p>So, my question is to save the last state of the command which has been executed... but what I'm getting as o/p is <strong>/Users/mac01</strong> after second pwd command execution, which is wrong.</p> <p>So, could anyone help me out with such type of problems..? Thanks in advance. I'm using libssh 2.0 library.</p> <p><strong>The method executing command:</strong></p> <p>char* cmd_exec(const char *commandline, const char *host, const char *username, const char *password, int port){ int sock, rc, bytecount = 0; char *cmd_contents;</p> <pre><code>if(!he) { struct sockaddr_in sin; </code></pre> <h1>ifdef WIN32</h1> <pre><code> WSADATA wsadata; WSAStartup(MAKEWORD(2,0), &amp;wsadata); </code></pre> <h1>endif</h1> <pre><code> /* Init and Make Socket Connection */ /* Start Socket Connection */ sock = socket(AF_INET, SOCK_STREAM, 0); </code></pre> <h1>ifndef WIN32</h1> <pre><code> fcntl(sock, F_SETFL, 0); </code></pre> <h1>endif</h1> <pre><code> sin.sin_family = AF_INET; sin.sin_port = htons(port); /*sin.sin_addr.s_addr = inet_addr(host); if (connect(sock, (struct sockaddr*)(&amp;sin), sizeof(struct sockaddr_in)) != 0) { // in case connection failure fprintf(stderr, "Internet connection is required!\n"); return "NETWORKFAILURE"; }*/ //const char *c = getIPFromHost("pepsi"); //sin.sin_addr.s_addr = inet_addr(c); /* IP Address Calculation */ he = gethostbyname(host); if(!he) return "Invalid hostname"; struct in_addr **addr_list; addr_list = (struct in_addr **)he-&gt;h_addr_list; //for(int i = 0; addr_list[i] != NULL; i++) { if(addr_list != NULL){ sin.sin_addr.s_addr = inet_addr(inet_ntoa(*addr_list[0])); //printf("%s", inet_ntoa(*addr_list[0])); if (connect(sock, (struct sockaddr*)(&amp;sin), sizeof(struct sockaddr_in)) != 0) { // in case connection failure fprintf(stderr, "Internet connection is required!\n"); return "NETWORKFAILURE"; } } } /* End Socket Connection */ // Initialize and create Session Instance if(!session) { session = libssh2_session_init(); if ( !session ) { fprintf( stderr, "Error initializing SSH session\n" ); return "SESSIONFAILURE"; } /* Since we have set non-blocking, tell libssh2 we are non-blocking */ //libssh2_session_set_blocking(session, 0); // Session starting if (libssh2_session_startup(session, sock)) { fprintf(stderr, "Failure establishing SSH session\n"); return "SESSIONFAILURE"; } /* Authenticate via password */ if(strlen(password) != 0){ if ( libssh2_userauth_password( session, username, password ) ) { fprintf( stderr, "Unable to authenticate user [%s]" "(wrong password specified?)\n", username ); return "AUTHENTICATIONFAILURE"; } }else{ while ((rc = libssh2_userauth_publickey_fromfile(session, 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"); return "AUTHENTICATIONFAILURE"; } } //libssh2_session_set_blocking(session, 1); } // Open a session channel for command execution if(!channel) { channel = libssh2_channel_open_session(session); if (!channel) { fprintf(stderr, "Unable to open a session\n"); return "SESSIONFAILURE"; } // Execute a command through channel while( (rc = libssh2_channel_shell(channel)) == //while( (rc = libssh2_channel_exec(channel, commandline)) == LIBSSH2_ERROR_EAGAIN ) { waitsocket(sock, session); } if( rc != 0 ) // if command execution failed { fprintf(stderr,"Error\n"); return "CMDFAILURE"; } } //libssh2_channel_write(channel,commandline,strlen(commandline)); do { /* write the same data over and over, until error or completion */ rc = libssh2_channel_write(channel, commandline, sizeof(commandline)); if (rc &lt; 0) { fprintf(stderr, "ERROR %d\n", rc); } } while (rc == 0); while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN); /* read channel output */ /* Start channel read */ for( ;; ) { /* loop until we block */ int rc; do { char buffer[0x4000]; // char *tcontents = (char*)malloc(sizeof(buffer) + sizeof(cmd_contents)); rc = libssh2_channel_read( channel, buffer, sizeof(buffer) ); if( rc &gt; 0 ) { int i; bytecount += rc; for( i=0; i &lt; rc; ++i ) fputc( buffer[i], stderr); if(cmd_contents){ free(cmd_contents); } cmd_contents = (char*)malloc(sizeof(buffer) + sizeof(cmd_contents)); strcpy(cmd_contents, buffer); fprintf(stderr, "\n"); } else { //fprintf(stderr, "libssh2_channel_read returned %d\n", rc); } } while( rc &gt; 0 ); /* this is due to blocking that would occur otherwise so we loop on this condition */ if( rc == LIBSSH2_ERROR_EAGAIN ) { waitsocket(sock, session); } else break; } /* End channel read */ while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ); /* closing channel */ int exitcode = 127; </code></pre> <p>// while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ); if( rc == 0 ) { exitcode = libssh2_channel_get_exit_status( channel ); } //<br> libssh2_channel_free(channel); // freeup memory channel = NULL;</p> <pre><code>/* libssh2_session_disconnect( session, "" ); // closing session libssh2_session_free( session ); // free up memory close( sock ); // closing socket */ return cmd_contents; </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload