I haven't been able to resolve this issue, but I suspect it's easy for someone familiar with Paramiko/ssh2 to figure out.
The code below works fine when executed only once, but when wrapped in a while loop it hangs on stdout.read(). I could not use exec_command because it was not returning the correct output (the device I am SSHing into is not a standard microcontroller, and I'm still uncertain exactly what encoding or ssh parameters it uses). Since this worked, I wanted to query the device continously, but it didn't work when wrapping the commands in a while loop.
I also tried changing how the while loop was wrapped, including wrapping the whole code block starting with the intial SSH connection, wrapping around channel.close, etc.
import paramiko
import time
freewave_shell = paramiko.SSHClient()
freewave_shell.set_missing_host_key_policy(paramiko.AutoAddPolicy())
freewave_shell.connect("an.ip.add.ress", username="user", password="pass")
chan = freewave_shell.invoke_shell()
while (1)
stdin = chan.makefile_stdin('wb')
stdout = chan.makefile('rb')
stdin.write('''
signalLevel
noiseLevel
signalMargin
VSWR
exit
''')
print('HERE')
print(stdout.read())
stdout.close()
stdin.close()
chan.close()
freewave_shell.close()
I do not think your code is anywhere near reliable.
But what's the primary issue is that if you close the I/O, you have to reconnect the channel. So you have to move the
invoke_shell
call into the loop.