Benjamin
Benjamin

Reputation: 25

Pexpect hanging on ssh connection

I've written a simple python script to connect (via ssh) to a Palo Alto firewall to gather some data via cli. However, once it connects to the box it won't execute the commands and it just hangs with the following error:

Traceback (most recent call last):

File "myscript.py", line 97, in

connectExpect()  

File "myscript.py", line 58, in connectExpect

child.expect('username@device-name> ')

File "/usr/lib/python2.7/site-packages/pexpect.py", line 1311, in expect

return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)

File "/usr/lib/python2.7/site-packages/pexpect.py", line 1325, in expect_list

return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)

File "/usr/lib/python2.7/site-packages/pexpect.py", line 1409, in expect_loop

raise TIMEOUT (str(e) + '\n' + str(self))

pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().

version: 2.3 ($Revision: 399 $)

command: /usr/bin/ssh

args: ['/usr/bin/ssh', 'user@{ip address}']

searcher: searcher_re:

0: re.compile("username@device-name> ")

buffer (last 100 chars): 2:14 2018 from {server name}

Welcome username.

username@device-name>

before (last 100 chars): 2:14 2018 from {server name}

Welcome username.

username@device-name>

after:

match: None

match_index: None

exitstatus: None

flag_eof: False

pid: 24490

child_fd: 3

closed: False

timeout: 30

delimiter:

logfile: None

logfile_read: None

logfile_send: None

maxread: 2000

ignorecase: False

searchwindowsize: None

delaybeforesend: 0.05

delayafterclose: 0.1

delayafterterminate: 0.1

Here is the script:

child = pexpect.spawn('ssh username@ipaddress')
child.expect('Password: ')
child.sendline('userpassword')
child.expect('username@device-name> ')  # User is now logged in 

the following commands don't get executed and the script hangs with

the error provided above

child.sendline('command1')
child.expect('username@device-name> ')
child.sendline('command2')

The user is still logged into the box at this time.

Upvotes: 0

Views: 853

Answers (1)

satyakrish
satyakrish

Reputation: 119

Can you try without the trailing whitespace in your expectation

child.expect('username@device-name>')

instead of

child.expect('username@device-name> ')  

Upvotes: 0

Related Questions