![]() |
Python Pexpect - SSH Timeout - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Python Pexpect - SSH Timeout (/thread-13454.html) |
Python Pexpect - SSH Timeout - Nirmal - Oct-16-2018 Hi I am using Pexpect module to make SSH connection but it is falling into Traceback and exits. Any idea how to fix it . I have tried multiple post that already exist in StackOverflow and other forums but end up with no luck. Traceback: <class 'pexpect.TIMEOUT'> Traceback (most recent call last): File "nirmal.hostlist.py", line 610, in <module> main() File "nirmal.hostlist.py", line 581, in main dataGrabber(ls5,ls5_leafs,username,password,enable_pass,ls5_cat_leafs,no_cat,no_l3_fw) File "nirmal.hostlist.py", line 428, in dataGrabber macs = macGrabber(child,switch) File "nirmal.hostlist.py", line 32, in macGrabber child.expect('.#') File "/usr/lib/python2.6/site-packages/pexpect.py", line 1311, in expect return self.expect_list(compiled_pattern_list, timeout, searchwindowsize) File "/usr/lib/python2.6/site-packages/pexpect.py", line 1325, in expect_list return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize) File "/usr/lib/python2.6/site-packages/pexpect.py", line 1409, in expect_loop raise TIMEOUT (str(e) + '\n' + str(self)) pexpect.TIMEOUT: Timeout exceeded in read_nonblocking(). <pexpect.spawn object at 0xc6af90> version: 2.3 ($Revision: 399 $) command: /usr/bin/ssh args: ['/usr/bin/ssh', 'ng044806@ls5mm33-ci-red'] searcher: searcher_re: 0: re.compile(".#") buffer (last 100 chars): before (last 100 chars): after: <class 'pexpect.TIMEOUT'> match: None match_index: None exitstatus: None flag_eof: False pid: 56774 child_fd: 3 closed: False timeout: 30 delimiter: <class 'pexpect.EOF'> logfile: None logfile_read: None logfile_send: None maxread: 2000 ignorecase: False searchwindowsize: None delaybeforesend: 0.05 delayafterclose: 0.1 delayafterterminate: 0.1My code: def ssh(hostname,username = 'nadda',password = 'nadda',enable_pass='nadda',cat = False): hostname = hostname.lower() pass_prompt = re.compile('.*assword:') need_key = re.compile('\s.*Are you sure you want to continue connecting \(yes\/no\)\?.',re.DOTALL) denied = re.compile('Permission denied, please try again\.') refused = re.compile('Connection refused by remote host') reset = re.compile('Connection reset by peer') count = 0 if username is 'nadda' and password is 'nadda': username = raw_input('login as: ') password = getpass.getpass('Password:') print "connecting to " + hostname if cat is True: print ('ssh -t root@mgmtsrver "ssh %s@%s"' % (username,hostname)) child = pexpect.spawn('ssh -t root@mgmtsrver "ssh %s@%s"' % (username,hostname)) #ssh -t root@mgmtsrver "ssh bs04628@fs-it-sw-blue" else: child = pexpect.spawn('ssh ' + username + '@' + hostname) print 'connected to ' + hostname i = child.expect([need_key,'.*>',pass_prompt,pexpect.TIMEOUT,denied,refused,reset]) if i==0: child.sendline('yes') print 'adding ssh key' child.expect(pass_prompt) else: print 'not adding ssh key' if i == 1 or cat is True: child.sendline(password) child.expect('.*>') child = enable(child,enable_pass) elif i == 2: if '-fw-' in hostname: child.sendline(enable_pass) child.expect('.*>') child = enable(child,enable_pass) else: child.sendline(password) child.sendline('term length 0') child.expect('.*#') elif i == 3: print 'SSH TIMED OUT ON ' + hostname exit() elif i == 4: print 'PERMISSION DENIED ON %s. Try re-running script with proper creds' % hostname child.close() exit() elif (i == 5 or i == 6) and count == 0: print 'CONNECTION REFUSED ON %s' % hostname print 'Will try to reconnect once in 5 seconds...' time.sleep(5) while count < 1: ssh(hostname,username,password,enable_pass) count += 1 elif (i == 5 or i == 6) and count > 0: print 'Sorry, %s refused your connection more than once. ' \ 'You\'ll have to re-run this script for %s' % (hostname, hostname[:3]) exit() print '\n' return child,username,password RE: Python Pexpect - SSH Timeout - wavic - Oct-16-2018 Test it in real using just bash and see what you get and why. Obviously pexpect doesn't read any output as I see in the documentation: https://pexpect.readthedocs.io/en/stable/overview.html#exceptions RE: Python Pexpect - SSH Timeout - Nirmal - Oct-17-2018 Got to know its getting failed here... def macGrabber(child,switch,cat = False): try: if cat is False: child.expect('.#') child.sendline('sh mac address-table | no-more') else: child.sendline('sh mac address-table dynamic | i Gi') child.expect('.#', timeout=3000) except pexpect.TIMEOUT: print child.before,child.after child.close() raise macs = child.before child.close() macs = macs.splitlines() print('Connection to %s CLOSED' % switch) return macsCan we set a retry here ? RE: Python Pexpect - SSH Timeout - wavic - Oct-17-2018 Set it where exactly? The function has return statement and no while loop to keep it running. You can call again any function you want if some conditions are or aren't met. |