Python Forum
facing problem with running interactive shell using paramiko
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
facing problem with running interactive shell using paramiko
#1
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  initializing interactive mode Skaperen 4 1,779 Jun-02-2024, 03:29 AM
Last Post: Skaperen
  clearing an interactive python interpreter/shell rootVIII 4 4,768 Jan-28-2019, 05:14 PM
Last Post: rootVIII

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020