When I'm in the Python shell (REPL?) I'm able to create a connection read from stdout of the SSH server. But when I run the same code as a script (via python3 -i script.py
) it is not working.
On the server side is a text-based MUD running. After loggin in via SSH it is asking for a MUD based login.
REPL
At the end you see that 153 lines where read.
>>> import paramiko
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname='fakehost', username='fakeuser', password='fakepassword')
>>> shell = client.invoke_shell()
>>> shell.setblocking(0)
>>> shell.send('username\n')
8
>>> shell.send('password\n')
10
>>> f = shell.makefile('r')
>>> r = []
>>> while shell.recv_ready():
... r.append(f.readline())
...
>>> print(f'Read {len(r)} lines.')
Read 153 lines.
As script
#!/usr/bin/env python3
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='fakehost', username='fakeuser', password='fakepassword')
shell = client.invoke_shell()
shell.setblocking(0)
shell.send('username\n')
shell.send('password\n')
f = shell.makefile('r')
r = []
while shell.recv_ready():
r.append(f.readline())
print(f'Read {len(r)} lines.')
The output here is just Read 1 lines.
. Where are the other 152 lines are gone?