0

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()
selamjie
  • 68
  • 8

1 Answers1

0

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Yes @Martin I did try wrapping around Invoke Shell which did not solve the issue. And yes, I am not generally familiar with Paramiko but I tried reading the documentation & other StackOverflow posts extensively and I was unable to figure this out...if there is a completely different approach which may work better I'd be happy to try and understand that. – selamjie Apr 16 '21 at 15:33