[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

using ssh_channel_write for command execution problem


Hi all,

I am trying to establish a persistent connection to a SSH server where I
want to periodically send commands via ssh_channel_write. I am using Linux
(5.0.0-32-generic #34~18.04.2-Ubuntu SMP).

My problem is when I call ssh_channel_write with a command I sometimes see
it executed on the server and sometimes not (but I think I don't have any
errors in logs). I am not sure what I am doing wrong. My code is very
simple and looks like this (for brevity I didn't include checking/error
handling, but I triple checked every call, they are all fine):

    ssh_init();
    ssh_session session = ssh_new();
    ssh_channel channel = nullptr;

    ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
    int verb = SSH_LOG_FUNCTIONS;
    int port = 22;
    ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verb);
    ssh_options_set(session, SSH_OPTIONS_PORT, &port);

    ssh_connect(session);

    ssh_userauth_password(session, "karah", "password");

    channel = ssh_channel_new(session);
    ssh_channel_open_session(channel);
    ssh_channel_request_pty(channel);
    ssh_channel_change_pty_size(channel, 220, 220);
    ssh_channel_request_shell(channel);
    char cmd[] = "touch ~/abcde\n"; //dummy command

    ssh_channel_write(channel, cmd, sizeof(cmd)); //I would like to call
this many times during the lifetime of channel
    ssh_blocking_flush(session, -1);
    ssh_channel_close(channel);
    ssh_channel_free(channel);
    ssh_disconnect(session);
    ssh_free(session);
    ssh_finalize();

In the attachment I am adding the SSH_LOG_FUNCTIONS output. If I brute
force the ssh_channel_write in an endless while loop like this:

    while(true)
        ssh_channel_write(channel, cmd, sizeof(cmd));

it eventually gets executed on the server. So I know the connection is ok
and probably I am calling the API in a wrong way.

I am obviously doing something wrong but I can't figure out what. Maybe I
am using the API in a wrong way or I misunderstood some key concept, I
don't know. Any help or hint would be highly appreciated.

Thanks!!

Attachment: logs
Description: Binary data


Follow-Ups:
Re: using ssh_channel_write for command execution problemAndreas Schneider <asn@xxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org