Sep-14-2017, 08:52 PM
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
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