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

Re: Getting error string from a failed ssh command


On 10/4/22 00:36, Norm Green wrote:
Hello libssh list,

If I run a command over ssh that fails from the Linux command line:

normg@moop>ssh hood foo
bash: foo: command not found
normg@moop>echo $?
127


I get an error string ("bash: foo: command not found") and an error code (127). For example:
My question is how to do the equivalent with libssh ?
If I do this after opening a session and a channel:

int rc = ssh_channel_request_exec(sshChannel, "foo");
int bytesReady = ssh_channel_poll_timeout(sshChannel, timeout, FALSE);

returns -127 and:

int numRead = ssh_channel_read_timeout(sshChannel,
                              resultBuffer.nextWriteByte(),
                              (int) resultBuffer.bytesFree(),
                              0, // timeout: block in poll, not here
                              0);

returns 0, so I don't get the error string back from libssh.
How can I get the error string result from the failed command using libssh ?

I think you need to set the `is_stderr` argument of the `ssh_channel_read_timeout()` to true to get this message as it will most likely be on the standard error instead of standard output you are querying.

Regards,
--
Jakub Jelen
Crypto Team, Security Engineering
Red Hat, Inc.


Follow-Ups:
Re: Getting error string from a failed ssh commandNorm Green <norm.green@xxxxxxxxxxxxxxxxxx>
References:
Getting error string from a failed ssh commandNorm Green <norm.green@xxxxxxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org