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 [email protected]
GPG-ID: 8DFF53E18F2ABC8D8F3C92237EE0FC4DCC014E3D