Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pexpect for interactive parsing failing?

Tags:

python

pexpect

I am trying to parse the bootup sequence of a device. I connect using telnet to the terminal server, which connects me to the serial port of the device.

The following script works and logs me in, which means the terminal is responsive to the script & sendline:

import pexpect
child = pexpect.spawn ('telnet x.x.x.x yyyy')
child.expect ('login: ')
child.sendline ('anonymous')
child.expect ('Password:')
child.sendline ('[email protected]')

The following fails:

import pexpect
child = pexpect.spawn ('telnet x.x.x.x yyyy')
child.expect ('Hit [Enter] to boot immediately, or space bar for command prompt.', timeout=300)
child.send ('\x20')
print child.before

My aim is when the device boots, it just does line-oriented output with my expected string coming somewhere in the middle of scroll.

The above script is unable to match. Upon further debugging "child.before" shows me last line prior to login prompt. What is the cause of the failure?

like image 208
rahul Verma Avatar asked Dec 04 '25 14:12

rahul Verma


1 Answers

Bingo! The escape character was what I was missing.

like image 85
rahul Verma Avatar answered Dec 07 '25 06:12

rahul Verma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!