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

Re: using ssh_channel_write for command execution problem


Hi Andreas,

I tried with ssh_channel_exec() (removing the PTY) and I am getting the
same behaviour.

I should also mention, that I am executing the executable from within a
docker container. The libssh logs show as if everything would be sent from
the libssh point of view, but in fact no command is executed on the ssh
server. It is purely stochastic in a sense that it sometimes works (i.e.
command is executed on the server). Just a as a sanity check - do you see
anything wrong in my code? It should be a vanilla libssh initialization and
a write. Could it be a unique issue in my environment?

Thanks!

št 7. 11. 2019 o 12:05 Andreas Schneider <asn@xxxxxxxxxxxxxx> napísal(a):

> On Wednesday, 6 November 2019 17:36:11 CET Karah Sekir wrote:
> > 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.
>
> Why don't you use ssh_channel_exec()? You don't seem to need a pty.
>
>
>         Andreas
>
> --
> Andreas Schneider                 asn@xxxxxxxxxxxxxx
> GPG-ID:     8DFF53E18F2ABC8D8F3C92237EE0FC4DCC014E3D
>
>
>

References:
using ssh_channel_write for command execution problemKarah Sekir <jackiechanaaa@xxxxxxxxx>
Re: using ssh_channel_write for command execution problemAndreas Schneider <asn@xxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org