python - paramiko combine stdout and stderr -


i trying combine output of stdout , stderr. belief can done set_combine_stderr() of channel object.

this doing:

ssh = paramiko.sshclient() #i connect , ok, then: chan = ssh.invoke_shell() chan.set_combine_stderr(true) chan.exec_command('python2.6 subir.py') resultado = chan.makefile('rb', -1.) 

however, following error when try store result (last line above, chan.makefile() ):

error: channel closed.

any appreciated

while true set_combine_stderr diverts stderr stdout stream, in chaotic order, not result want, namely, lines combined in order written, if running command in local terminal window. instead, use get_pty. cause server run lines through pseudo-terminal, keeping them in chronological sequence.

here's test program, outerr.py, writes alternating lines on stdout , stdin. assume it's sitting in home directory of llmps@meerkat2.

#!/usr/bin/env python  import sys  x in xrange(1, 101):     (sys.stdout, sys.stderr)[x%2].write('this line #%s, on std%s.\n' %                 (x, ('out', 'err')[x%2])) 

now try following code run remotely:

#!/usr/bin/env python  import paramiko  def connect():     ssh = paramiko.sshclient()     ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())     ssh.connect('meerkat2', username='llmps', password='..')     return ssh  def runtest(ssh):     tran = ssh.get_transport()     chan = tran.open_session()     # chan.set_combine_stderr(true)     chan.get_pty()     f = chan.makefile()     chan.exec_command('./outerr.py')     print f.read(),  if __name__ == '__main__':     ssh = connect()     runtest(ssh)     ssh.close() 

if run above, should see 100 lines in order written. if, instead, comment out chan.get_pty() call , uncomment chan.set_combine_stderr(true) call, clumps of stdout , stderr lines interspersed randomly run run.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -