I would like to be able to control a remote Python interpreter over an SSH connection, and drive it from Python itself.
I’ve got a basic template:
ssh.connect(servername, serverport, username, key_filename=key_filename)
transport = ssh.get_transport()
channel = transport.open_session()
channel.exec_command(PATH_TO_EXEC)
while True:
r, w, e = select.select([channel], [], [], 1)
if channel in r:
try:
if channel.recv_ready():
x = channel.recv(64)
elif channel.recv_stderr_ready():
x = channel.recv_stderr(64)
else:
continue
if len(x) == 0:
print '\r\n*** EOF\r\n',
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
which allows me to talk to the remote application with pdb: channel.set("command\n").
It works perfectly with bash, with gdb, but there is nothing I can do to get an output stream from python (v2)
How does Python handle its output stream, why my code doesn’t work with it?
Unless this is an academic exercise, or you have some specific requirement to use ssh, have a look at pushy. I’ve never used it but it seems mature.