Subject: Re: SSH client is not reading full data if executed command generates big output

Re: SSH client is not reading full data if executed command generates big output

From: Peter Stuge <peter_at_stuge.se>
Date: Sun, 28 Oct 2018 20:49:26 +0000

Subrata Dasgupta wrote:
> My intention is to run multiple commands sequentially over ssh and
> get the output.It seems &quot;libssh2_channel_exec&quot; function
> can only execute a single command over a session or channel. So
> this option will not serve my purpose because commands may have
> some sub-commands. So I need to retain the session or channel to
> execute those sub-commands. Please let me know if my
> understanding is wrong and kindly let me know how to execute
> multiple commands and sub-commands over libssh2_channel_exec.

Your understanding is correct; libssh2_channel_exec() only ever
executes one command, but you can call it many times within one
session.

That allows you to take advantage of well-defined fit-for-purpose
behavior and interactions between your client and the server. This
way, your software has some chance to control the processes on the
server side.

> It seems only &quot;libssh2_channel_shell&quot; function can serve
> my purpose because I can execute multiple commands on a established
> channel / session. Please let me know if I am wrong.

A shell channel is not needed to execute multiple commands in one
session, only to execute multiple commands in one *channel*. Study
the difference to see what is actually required in your case.

> It also seems from the different ssh tutorials from net that
> non-interactive session may serve my purpose because there is no
> pty associated with the channel. So I have tried to comment out
> the &quot;libssh2_channel_request_pty&quot; call , but unfortunately!
> result is the same(server do not send all data). What could be
> the reason of such problem and how to fix it if possible ?

One reason could be that the software you are executing on the server
simply is not written to support both interactive and programmed use.
You can't fix that in the client. There can be other reasons, you'll
have to study the particular server software you want to support in
detail.

> Lastly is there anyway to get all output data of commands or
> sub-commands without using terminal emulation ??

Please clarify what you mean by "sub-commands" ?

In general, if the software you want to execute on the server does
not explicitly support programmed use (and this is likely the case)
then your only option is to write a software that simulates interactive
use, which neccessarily requires terminal emulation to handle everything
that the server software outputs, as well as everything that your
simulator requires to output.

Shells are human interfaces, not programming interfaces, making them a
poor choice for automation. Sometimes there is may be no other way,
but it always requires a lot of (I think wasted) effort. Try to solve
the problem another way if possible.

//Peter
_______________________________________________
libssh2-devel https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2018-10-28