EldoS | Feel safer!

Software components for data protection, secure storage and transfer

TEISimpleSSHClient.ExecuteCommand - how to get stdout and stderr?

Posted: 03/22/2013 13:37:08
by Michael Lovett (Standard support level)
Joined: 03/20/2013
Posts: 28

I'm testing ExecuteCommand() like this:

byte[] myStderr = null;
byte[] myStdout = null;

myStdout = Ssh.ExecuteCommand("cd ~; python outputs.py", ref myStderr);

The script being run here (outputs.py) writes to both stdout and stderr, but ExecuteCommand is not returning those two items separately; the script's stdout and stderr output are being concatenated by ExecuteCommand and assigned to myStdout, while myStderr is empty.

What do I need to do to get both stdout and stderr to be returned separately and not concatenated?

(I see there's another overload to ExecuteCommand that accepts a flag RedirectStdErr but it's not clear to me how that works)

Posted: 03/22/2013 15:29:34
by Ken Ivanov (Team)


TElSimpleSSHClient only merges stderr and stdout outputs if RedirectStdErr parameter is set to true (and it is not in your case). That is, if you've got the outputs concatenated after the above call, the concatenation is performed on the server side. Please check with the server documentation and/or configuration file to find out if it can send stdout and stderr data separately.
Posted: 03/22/2013 15:41:42
by Michael Lovett (Standard support level)
Joined: 03/20/2013
Posts: 28

Well, here is my server side script, in python:

import sys

sys.stdout.write("This output is for stdout")
sys.stderr.write("This output is for stderr")

To make sure this script is indeed writing to both stdout and stderr, I did this from the command line on that server

python myscript.py > x 2> z

In file x I see "This output is for stdout"
In file z I see "This output is for stderr".

Unless I'm overlooking something here, I think this proves my script writes separately to stdout and to stderr, yet ExecuteCommand is only returning one string which contains ("This output is for stdout This output is for stderr).

Any thoughts?
Posted: 03/22/2013 15:56:31
by Michael Lovett (Standard support level)
Joined: 03/20/2013
Posts: 28

One thing I wonder here is whether or not I'm correctly supply the byte[] buffers that are being passed to ExecuteCommand() ? The docs did not say whether or not these buffers were expected to be set to null, or whether they should have storage allocated to them before I called ExecuteCommand()...

I wonder if maybe ExecuteCommand is not happy with the null buffer being passed in and therefore is maybe deciding the only way to supply stderr is to tack it on to the return value?

I'm totally guessing here..

Posted: 03/22/2013 16:37:59
by Ken Ivanov (Team)

I am not blaming your Python script (it is quite correct); instead it looks like the SSH server merges the stdout and stderr outputs and sends both of them through the stdout channel. There can be a server setting responsible for this aspect of its behaviour. Alternatively, you can try to invert the value of the RequestTerminal property - it helps sometimes.

And you are doing everything correctly with regard to buffer management. The buffers are allocated inside the ExecuteCommand() method, so you do not need to allocate them before calling ExecuteCommand().
Posted: 03/25/2013 07:26:18
by Michael Lovett (Standard support level)
Joined: 03/20/2013
Posts: 28

Setting RequestTerminal to false fixed this problem. Thanks for the reply :)



Topic viewed 1157 times

Number of guests: 1, registered members: 0, in total hidden: 0


Back to top

As of July 15, 2016 EldoS business operates as a division of /n software, inc. For more information, please read the announcement.

Got it!