www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples | github

Archive Index This month's Index

Subject: Re: Login problem - Power Broker Menu

Re: Login problem - Power Broker Menu

From: Naveen Sharma <writetonsharma_at_gmail.com>
Date: Thu, 3 Mar 2011 18:36:41 +0530

On Tue, Mar 1, 2011 at 8:45 PM, Peter Stuge <peter_at_stuge.se> wrote:
> Naveen Sharma wrote:
>> This is the sequence of code I am trying:
>>
>> libssh2_channel_open_session()
>> libssh2_channel_request_pty()
>> libssh2_channel_shell()
>> libssh2_channel_read()               // This is reading this menu.
>>
>> I am trying, libssh2_channel_write(), but this is not working.
>> Please suggest a solution.
>
> You have not provided very much information, so it is difficult to
> suggest anything.
>
> For a start, please send the exact bytes that you receive from the
> server. You are clearly not having a problem with the SSH protocol
> here, I think it is more likely that your program is not emulating
> the terminal type you claimed to support in _request_pty() completely
> or that the terminal type is not compatible with the server.
>
> Of course full libssh2 debug output would also work. Recompile the
> library specifying --enable-debug during configure and call
> libssh2_trace(session, ~0); and your program should produce very
> verbose output that will help us find your problem.
>
>
> //Peter
> _______________________________________________
> libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
>

Thanks Peter for pointing me in some direction. I have tried
libssh2_trace(), but was not able to solve my problem.
Let me try to explain the problem in detail.

here is the relevant part of code which I am trying to use and is
taken from the examples of libssh2.

//Authentication is successful before this code.

/* Request a shell */
    if (!(channel = libssh2_channel_open_session(session))) {
        fprintf(stderr, "Unable to open a session\n");
        goto shutdown;
    }

/* Request a terminal with 'vanilla' terminal emulation
     * See /etc/termcap for more options
     */
    if (libssh2_channel_request_pty(channel, "ansi")) {
        fprintf(stderr, "Failed requesting pty\n");
        goto skip_shell;
    }

    /* Open a SHELL on that pty */
    if (libssh2_channel_shell(channel)) {
        fprintf(stderr, "Unable to request shell on allocated pty\n");
        goto shutdown;
    }

/* At this point the shell can be interacted with using
     * libssh2_channel_read()
     * libssh2_channel_read_stderr()
     * libssh2_channel_write()
     * libssh2_channel_write_stderr()
     *
     * Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking()
     * If the server send EOF, libssh2_channel_eof() will return non-0
     * To send EOF to the server use: libssh2_channel_send_eof()
     * A channel can be closed with: libssh2_channel_close()
     * A channel can be freed with: libssh2_channel_free()
     */

        do
        {
in:
                memset(buff_read,0,sizeof(buff_read));
                rc == libssh2_channel_read(channel, buff_read, sizeof(buff_read));
                fprintf (stderr, "%s", buff_read);
                                  // this prints the whole menu like
below.
        } while (rc > 0);
        if( rc == LIBSSH2_ERROR_EAGAIN )
        {
                waitsocket(sock, session);
                goto in;
        }

At this point, I am able to read the menu which asks the user to give replies.
1) Power Broker Menu
2) Change Power Broker Password
3) Change Password on this server
0) Exit

Please enter choice:

In a typical ssh session, a user will give "1" and will press enter
and will enter the shell.
I have to somehow give a "1" from the code and read what comes next.
So what I am doing is:

rc = libssh2_channel_write(channel, "1", strlen("1"));
rc = libssh2_channel_send_eof(channel);

I am doing this to send a "1" to the shell but this is not sending a
"1". I have no idea how to do this. If this is successful, then I will
read what is returned, print on the console will enter the shell.

I hope this would be more clear, what I am trying to do.

Thanks,
Naveen.

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2011-03-03

the libssh2 team