1

EDIT: Here's the complete code. modified the code to work with a router but the use case is same. Once i issue the password using libssh2_channel_write() subsequent libssh2_channel_read() fails with LIBSSH2_ERROR_SOCKET_RECV. Not sure why. I am unable to send subsequent commands to the remote device and get their output.

Logic was to execute a command on the remote device ( libssh2_channel_exec ). This command execution would throw a password to be entered by the client. Now read the stream via libssh2_channel_read() and ensure that the password is being asked and write the password to the channel via libssh2_channel_write(). Ensure the password is accepted on the remote device by doing subsequent reads [ THIS IS WHERE THE LIB IS FAILING WITH ERROR_SOCKET_RECV ] and then send the command to be executed via libssh2_channel_write() and read the command output. Am i missing something ?

    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;
    }
  • If you're using libssh2 : you'll need to post your corresponding code... – user2284570 Apr 11 '14 at 23:45
  • thanks for looking into the code. i have dropped a detailed note. – user3525582 Apr 29 '14 at 04:50
  • No, This is part of StackOverFlow rules : You must include a *minimal* part of your code if you're asking to resolve problems with it. – user2284570 Apr 29 '14 at 07:22
  • I am new to this list and dint know that, apologies. i have edited the code to include the part that has the problem. Once i do a libssh2_channel_write() to send the password, subsequent libssh2_channel_read() fails with LIBSSH2_ERROR_SOCKET_RECV. Idea is to send the password, ensure the passwd is right by reading the channel ( will get a device prompt if passwd is right ) and then send the command to execute on the device and do a subsequent read to get the device output. Am i missing something ? – user3525582 Apr 29 '14 at 16:52
  • [I don't know](http://stackoverflow.com/review/first-posts "http://stackoverflow.com/review"). – user2284570 Apr 29 '14 at 17:16

0 Answers0