Paramiko models the ssh -t parameter

I create a paramiko channel, then execute the command and get its output:

channel = transport.open_session() channel.exec_command('service myservice restart') stdout = channel.makefile('rb') for line in stdout: print line, 

However, after executing the command (which ends), the iteration of the output is blocked.

I tested with ssh:

 ssh myhost service myservice restart # terminal gets blocked ssh -t myhost service myservice restart # OK 

So, I want to simulate the -t option in paramiko. So far I have tried:

 channel = transport.open_session() channel.get_pty() channel.invoke_shell() stdin, stdout = channel.makefile('wb'), channel.makefile('rb') stdin.write('service myservice restart\n') for line in stdout: print line, 

But now stdout does not close, and for never ends.

Any ideas?

+4
source share
2 answers

invoke_shell() seems to return a Channel , and it looks like Channel requires you to close them explicitly. I tried to close some of the channels that you open, in particular the one returned by invoke_shell() .

0
source

Look at the script you are trying to run, see if there are lines like this

 /dev/null 2>&1 

I have the same problem as you - in my case, trying to remotely run the bitnami script control. Something in your post ran through my memory and reminded me of the redirection of the output that are in the script control (this caused me some serious headache before).

In general, theyre either used to ignore errors, or perhaps write them down somewhere specific - I didn’t have the opportunity to try it yet, but maybe bring them back to the end of the script or if you do not care about the answer, even manually redirecting some created data β†’ 2 will work.

-1
source

All Articles