Thursday, April 29, 2021

Need help with Pexpect for device config

Hello,

I am still learning Python for networking :) but can't figure this out. If someone can help would really be thankful. My goal is to pull the 'show run' from the list of devices and create the list of VLANs for every device. Script successfully running show run but not the VLAN configuration. Most certainly it's something wrong with for loop. Any ideas? Thank You

import pexpect import sys Cisco_IOS = ['192.168.122.72', '192.168.122.82'] Arista_EOS = ['192.168.122.83', '192.168.122.84'] user = 'cisco' passwd = 'cisco' command = 'show run' for Cisco_device in Cisco_IOS: ch = pexpect.spawn('ssh %s@%s' % (user, Cisco_device)) ch.logfile = sys.stdout.buffer ch.expect('Password') ch.sendline(passwd) ch.expect('#') ch.sendline('terminal length 0') ch.expect('#') ch.sendline(command) ch.expect('#') for n in range(2, 11): print("Creating VLAN " + str(n)) ch.sendline('conf t') ch.expect('\(config\)#') config_commands = ['vlan %d' + str(n)] #issue here config_name = ['name Pexpect_VLAN_%d' + str(n)] #issue here ch.sendline(config_commands) ch.expect('\(config-vlan\)#') ch.sendline(config_name) ch.expect('\(config-vlan\)#') ch.sendline('end') for Arista_device in Arista_EOS: ch = pexpect.spawn('ssh %s@%s' % (user, Arista_device)) ch.logfile = sys.stdout.buffer ch.expect('Password') ch.sendline(passwd) ch.expect('>') ch.sendline('enable') ch.expect('#') ch.sendline('terminal length 0') ch.expect('#') ch.sendline('show run') ch.expect('#') x = ch.before.decode('utf-8').splitlines() ch.sendline('exit') for line in x: print(line) 


No comments:

Post a Comment