4
votes

I instantiate a paramiko channel, then I execute a 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 finishes), the output iterating gets 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 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 doesn't get closed, and the for never ends.

Any ideas?

2

2 Answers

0
votes

It appears like invoke_shell() returns a Channel, and it looks like Channels require that you close them explicitly. I would attempt to close some of the channels you're opening, in particular the one returned by invoke_shell().

-1
votes

Have a look at the script that youre trying to run- see if there are any lines like this

/dev/null 2>&1

Im having the same issue as you- in my case trying to remotely run a bitnami control script. Something in your post jogged my memory and reminded me of the output redirections that are in the control script (these caused me some major headache before).

Generally theyre used to either ignore errors or maybe log them somewhere specific- I havent had a chance to try yet, but maybe either piping them back out at the end of the script or if you dont care about the response maybe even manually redirecting some created data out >&2 would work.