Python Forum
Getting EOF error with pexpect
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting EOF error with pexpect
#1
Hi, I'm working on a script to;

log into a network device > get info of the interfaces > apply interface specific config.

This code works fine on 2 other devices, on the device which fails there is a larger output of <show ip interface brief> so a user at the CLI would normally press SPACEBAR to advance to interface list.

My code looks like this;

#
import pexpect
import getpass
#
device_ip = raw_input('Please Enter Device IP Address: ')
uname = raw_input('Please Enter Username: ')
pword = getpass.getpass('Please Enter Password: ')
#
child = pexpect.spawn('ssh -o "StrictHostKeyChecking no" ' + uname + '@' + device_ip)
child.expect('Password:')
child.sendline(pword)
child.expect('#')
child.sendline('show ip interface brief')
child.expect('#')
#
show_ip_int_brief = child.before
#
interface_list =
#
for line in show_ip_int_brief.splitlines()[2:-1]:
current_interface = line.split ()[0]
if ('GigabitEthernet' in current_interface or
'FastEthernet' in current_interface or
'Vlan' in current_interface):
interface_list.append (current_interface)
#
print'Interface Configuration Will Be Applied To: ', interface_list
#
child.sendline('configure terminal')
child.expect('#')
#
for int in interface_list:
child.sendline('interface ' + int)
child.sendline('description ' + int)
#
child.sendline('end')
child.sendline('exit')

Error message below

Traceback (most recent call last):
File "temp.py", line 14, in <module>
child.expect('#')
File "/Library/Python/2.7/site-packages/pexpect-4.2.1-py2.7.egg/pexpect/spawnbase.py", line 321, in expect
timeout, searchwindowsize, async)
File "/Library/Python/2.7/site-packages/pexpect-4.2.1-py2.7.egg/pexpect/spawnbase.py", line 345, in expect_list
return exp.expect_loop(timeout)
File "/Library/Python/2.7/site-packages/pexpect-4.2.1-py2.7.egg/pexpect/expect.py", line 107, in expect_loop
return self.timeout(e)
File "/Library/Python/2.7/site-packages/pexpect-4.2.1-py2.7.egg/pexpect/expect.py", line 70, in timeout
raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0x10695aa50>
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '-o', 'StrictHostKeyChecking no', '[email protected]']
buffer (last 100 chars): 'wn \r\nGigabitEthernet1/0/16 unassigned YES unset down down \r\n --More-- '
before (last 100 chars): 'wn \r\nGigabitEthernet1/0/16 unassigned YES unset down down \r\n --More-- '
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 3409
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
0: re.compile("#")

Thanks
Reply
#2
Sorry, with correct formatting. Thanks for the pointer.

#
import pexpect
import getpass
#
device_ip = raw_input('Please Enter Device IP Address: ')
uname = raw_input('Please Enter Username: ')
pword = getpass.getpass('Please Enter Password: ')
#
child = pexpect.spawn('ssh -o "StrictHostKeyChecking no" ' + uname + '@' + device_ip)
child.expect('Password:')
child.sendline(pword)
child.expect('#')
child.sendline('show ip interface brief')
child.expect('#')
#
show_ip_int_brief = child.before
#
interface_list = []
#
for line in show_ip_int_brief.splitlines()[2:-1]:
   current_interface = line.split ()[0]
   if ('GigabitEthernet' in current_interface or
      'FastEthernet' in current_interface or
      'Vlan' in current_interface):
       interface_list.append (current_interface)
#
print'Interface Configuration Will Be Applied To: ', interface_list
#
child.sendline('configure terminal')
child.expect('#')
#
for int in interface_list:
	child.sendline('interface ' + int)
	child.sendline('description ' + int)
#
child.sendline('end')
child.sendline('exit')
Reply
#3
Rather unfriendly response from a moderator, thanks, won't be using the forum again
Reply
#4
Please do not PM moderators. Share with all here.
The reference was to BBCODE which explaing how to use code tags.
Looks like you found it.
And the second item was for preservation of indentation, so that the code can be copied and run as is.

It was not meant to be unfriendly, just pointer as to how to properly post and follow forum rules.

I think you will agree that the re-post looks much better.
Reply
#5
For anyone that comes across a similar problem to mine - the problem has been (partially) resolved with;

child.setwinsize

I now have other problems but no longer get the apparent buffering problem.
Reply
#6
Thanks for sharing your fix for future visitors :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 4,218 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Use pexpect to send user input alisha17 0 1,886 May-10-2022, 02:44 AM
Last Post: alisha17
  Not able to read the text using pexpect/expect Bipinjohnson 7 4,025 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  Sudden Problem with pexpect gw1500se 3 2,381 Nov-19-2021, 11:21 PM
Last Post: bowlofred
  How to use pexpect in python? tiho_bg 1 1,524 Oct-30-2021, 02:50 PM
Last Post: Yoriz
  Pexpect timesout before executing whole output eagerissac 0 1,481 Jun-23-2021, 03:30 AM
Last Post: eagerissac
  pexpect startup help korenron 2 3,473 Apr-27-2021, 07:23 AM
Last Post: korenron
  Problem with pexpect.exception.TimeOUT korenron 0 3,287 Apr-12-2021, 03:25 PM
Last Post: korenron
  Request help on pexpect rsurathu 4 5,785 Jul-19-2020, 03:35 PM
Last Post: snippsat
  SIGWINCH ignored when trying to start a screen with the pexpect module StatTark 0 1,822 Jul-06-2020, 10:12 AM
Last Post: StatTark

Forum Jump:

User Panel Messages

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