Python Forum
facing problem with running interactive shell using paramiko - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: facing problem with running interactive shell using paramiko (/thread-641.html)



facing problem with running interactive shell using paramiko - saswatidas437 - Oct-26-2016

I have written below code to run 3 command in remote server interactively

But when i checked 3rd command never executed and code stuck here is my code

def execute():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('ipaddress',username='user', password='pw')

chan=ssh.invoke_shell()    # start the shell before sending commands

chan.send('cd /path to folder/test')
chan.send('\n')
time.sleep(3)    
chan.send("ls -l")
chan.send('\n')
buff=''
while not buff.endswith(">"):
        resp = chan.recv(9999)
       # code stuck here after 'path to folder/test >' comes in shell prompt
        buff+=resp
        print resp

print "test"
chan.send("ls -lh")
chan.send('\n')
time.sleep(5) 
buff=''
while not buff.endswith(">"):
    resp = chan.recv(9999)
    buff+=resp
    print resp         

if __name__ == "__main__":
    execute()           


When i ran i got output of ls -l but ls -lh never executed my code stuck in first while loop. Anyone please help to resolve my issue



RE: facing problem with running interactive shell using paramiko - Larz60+ - Oct-26-2016

Put a couple of print statements to print resp and buff.
In addition, put the while loop code in a try except loop that way you'll capture any errors.

If you can set a timeout, that may help, as it may give you a timeout message
(hopefully something more verbose than ... timed out)