· 7 years ago · Apr 26, 2018, 04:02 PM
1import cli
2
3map = [
4 { 'sn': 'abc12134', 'ip': '192.168.1.11', 'sm': '255.255.255.0'},
5 { 'sn': 'abc12135', 'ip': '192.168.1.12', 'sm': '255.255.255.0'},
6]
7
8# determine switch serial number (via cli or some other method)
9def get_serials():
10 # xml formatted
11 inventory = cli('show inventory | format')
12 # skip leading newline
13 doc = minidom.parseString(inventory[1:])
14 serials =[]
15 for node in doc.getElementsByTagName('InventoryEntry'):
16 # router, what if there are several?
17 chassis = node.getElementsByTagName('ChassisName')[0]
18 if chassis.firstChild.data == "Chassis":
19 serials.append(node.getElementsByTagName('SN')[0].firstChild.data)
20
21 # switch
22 match = re.match('"Switch ([0-9])"', chassis.firstChild.data)
23 if match:
24 serials.append(node.getElementsByTagName('SN')[0].firstChild.data)
25
26 return serials
27
28targets = get_serials()
29
30for m in map:
31 for target in targets:
32 if m['sn'] == target: # our switches serial number
33 # run commands
34 #cli("enable", "R3vr$e", "configure terminal", "ip routing", "interface vlan4", "no switchport",
35 # "ip address" + " " + m['ip'] + " " + m["sm"], "no shutdown")
36 configure('ip routing')
37 configure('interface vlan 4',
38 'no switchport',
39 ('ip address {} {}'.format(m['ip'], m['sm'])),
40 'no shut')