Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paramiko combine stdout and stderr

I am trying to combine the output of stdout and stderr. My belief is that this can be done with the set_combine_stderr() of a Channel object.

This is what I am doing:

SSH = paramiko.SSHClient()
#I connect and everything OK, then:
chan = ssh.invoke_shell()
chan.set_combine_stderr(True)
chan.exec_command('python2.6 subir.py')
resultado = chan.makefile('rb', -1.)

However, I get the following error when I try to store the result (last line above, chan.makefile() ):

Error: Channel closed.

Any help would be greatly appreciated

like image 576
DanielS Avatar asked Sep 29 '10 16:09

DanielS


1 Answers

Ok, I know this is quite an old topic, but I run into the same problem and I got a (maybe not-so-)pretty solution. Just call the command on the remote server redirecting the stderr to stdout and then always read from the stdout. For example:

client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', username='user', password='pass')

stdin,stdout,stderr = client.exec_command('python your_script.py 2> \&1')
print stdout.read()
like image 196
robotic_chaos Avatar answered Sep 22 '22 18:09

robotic_chaos