-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrom_Nexus_to_excel_vlan_list.py
172 lines (161 loc) · 7.22 KB
/
from_Nexus_to_excel_vlan_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import openpyxl
import re
import os.path
# directory where the excel file will be created
excel_dir = "C:/Users/my_output_directory/"
# excel filename, will be created in the above directory
file_name = "ACI_vlan_list.xlsx"
# directory where Nexus configurations will be read
config_dir = "C:/Users/customer_configurations_path/"
# Nexus configuration filenames
config_files = ["Nexus_config_1.txt", "Nexus_config_2.txt", "Nexus_config_3.txt"]
if not os.path.isfile(excel_dir + file_name):
targ = openpyxl.Workbook()
else:
targ = openpyxl.load_workbook(excel_dir + file_name)
if 'Network' in targ.sheetnames:
sheet = targ.get_sheet_by_name("Network")
else:
sheet = targ.create_sheet("Network")
VLAN = 0
VRF = ""
IP_ADDRESS = ""
description = ""
HSRP_GROUP = ""
HSRP = ""
if_list = {}
vrf = ""
counter = 1
sheet['A'+str(counter)].value = "Apparato"
sheet['B'+str(counter)].value = "vrf"
sheet['C'+str(counter)].value = "vlan #"
sheet['D'+str(counter)].value = "l2 vlan name"
sheet['E'+str(counter)].value = "ip address"
sheet['F'+str(counter)].value = "hsrp grp"
sheet['G'+str(counter)].value = "hsrp vip"
sheet['H'+str(counter)].value = "description"
# L2_ONLY vlans are NOT printed in this
for read_file in config_files:
if_list[read_file] = {}
l2_vlan_names = {}
l2_only_vlans = {}
# list of temporary variables to save all data
VLAN = 0
VRF = ''
PORT_MODE = ''
TRUNK = ''
IP_ADDRESS = ''
description = ''
L2_VLAN = 0
HSRP_GROUP = ''
HSRP = ''
config = open(config_dir + read_file,"r")
for line in config:
if (re.search("^vlan (\d+)", line)):
L2_VLAN = re.search("^vlan (\d+)", line).group(1)
# print("inside vlan "+L2_VLAN)
elif (re.search("^\s+name (.*)\s", line) and L2_VLAN):
l2_vlan_names[L2_VLAN] = re.search("^\s+name (.*)\s", line).group(1)
l2_only_vlans[L2_VLAN] = re.search("^\s+name (.*)\s", line).group(1)
# print ("inside name "+l2_vlan_names[L2_VLAN])
elif re.search("^interface (.*)\s", line):
VLAN = re.search("^interface (.*)\s", line).group(1)
#print('Inside interface '+str(VLAN))
elif re.search("^\s+$", line):
#print('Ended interface configuration '+str(VLAN)+' '+PORT_MODE+' '+TRUNK)
# exited from configuration block, VLAN contains the physical interface
# or the interface vlan.
if (VLAN):
if (re.search("Vlan", VLAN)):
VLAN_NUM = re.search("Vlan(\d+)", VLAN).group(1)
if not (re.search("Vlan", VLAN)):
VLAN_NUM = 0
elif ((VLAN_NUM in l2_vlan_names) and re.search("^TR-",l2_vlan_names[VLAN_NUM])):
print('Excluded transit '+VLAN+' \"'+l2_vlan_names[VLAN_NUM]+'\"')
elif (re.search("ansit",description.lower())):
print("Excluded transit vlan "+VLAN+" "+description)
else:
if not VRF in if_list[read_file]:
if_list[read_file][VRF] = {}
if not VLAN in if_list[read_file][VRF]:
if_list[read_file][VRF][VLAN] = {}
if_list[read_file][VRF][VLAN]['ip address'] = IP_ADDRESS
# let's save the values on the excel file
counter += 1
sheet['A'+str(counter)].value = read_file
sheet['B'+str(counter)].value = VRF
# this could be also the physical interface
sheet['C'+str(counter)].value = VLAN
if (VLAN_NUM in l2_vlan_names):
sheet['D'+str(counter)].value = l2_vlan_names[VLAN_NUM]
l2_only_vlans.pop(VLAN_NUM)
elif (L2_VLAN in l2_vlan_names):
sheet['D'+str(counter)].value = l2_vlan_names[L2_VLAN]
elif (PORT_MODE == 'trunk' and TRUNK!=None):
sheet['D'+str(counter)].value = TRUNK
sheet['E'+str(counter)].value = IP_ADDRESS
sheet['F'+str(counter)].value = HSRP_GROUP
sheet['G'+str(counter)].value = HSRP
sheet['H'+str(counter)].value = description
# we exited from a configuration 'step', let's erase all the saved parameters
VLAN = 0
VRF = ''
PORT_MODE = ''
TRUNK = ''
IP_ADDRESS = ''
description = ''
L2_VLAN = 0
HSRP_GROUP = ''
HSRP = ''
# we are inside an interface configuration, being it a physical one or a vlan
if (VLAN):
if re.search("^\s+vrf member\s+",line):
VRF = re.search("vrf member (.*?)\s",line).group(1)
elif re.search("^\s+description", line):
description = re.search("^\s+description (.*)\s+",line).group(1)
elif re.search("ip address", line):
IP_ADDRESS = re.search("ip address (.*)\s",line).group(1)
elif re.search("^\s+hsrp\s+\d+", line):
HSRP_GROUP = re.search("^\s+hsrp\s+(\d+)", line).group(1)
elif re.search("\s+ip\s+\d+\.\d+\.\d+\.\d+", line):
mask = re.search("\/(\d+)", IP_ADDRESS).group(1)
HSRP = re.search("ip\s+(\d+\.\d+\.\d+\.\d+)", line).group(1)+"/"+mask
elif re.search('switchport access vlan \d+', line):
L2_VLAN = re.search('switchport access vlan (\d+)', line).group(1)
elif re.search('switchport access vlan \d+', line):
L2_VLAN = re.search('switchport access vlan (\d+)', line).group(1)
elif re.search('switchport mode', line):
PORT_MODE = re.search('switchport mode (.*?)\s', line).group(1)
elif re.search('^\s+switchport trunk allowed vlan add \d', line):
TRUNK += ','+re.search('switchport trunk allowed vlan add (.*?)\s', line).group(1)
elif re.search('^\s+switchport trunk allowed vlan \d', line):
TRUNK = re.search('switchport trunk allowed vlan (\d.*?)\s', line).group(1)
# printing out all L2-only vlans ...
for vlan in sorted(l2_only_vlans):
counter += 1
sheet['A'+str(counter)].value = read_file
sheet['C'+str(counter)].value = vlan
sheet['D'+str(counter)].value = l2_vlan_names[vlan]
sheet['H'+str(counter)].value = 'L2 only vlan'
if 'Vlan_summary' in targ.sheetnames:
sheet = targ.get_sheet_by_name("Vlan_summary")
else:
sheet = targ.create_sheet("Vlan_summary")
counter=1
sheet['A'+str(counter)].value = "Apparato"
sheet['B'+str(counter)].value = "vlan"
sheet['C'+str(counter)].value = "vrf"
sheet['D'+str(counter)].value = "#host_ip"
for router in if_list:
for vrf in if_list[router]:
counter+=1
sheet['A'+str(counter)].value = router
sheet['B'+str(counter)].value = str(len(if_list[router][vrf]))
sheet['C'+str(counter)].value = vrf
tot_ip = 0
for vlan in if_list[router][vrf]:
if (re.search("\/(\d+)",if_list[router][vrf][vlan]['ip address'])):
mask = (int)(re.search("\/(\d+)",if_list[router][vrf][vlan]['ip address']).group(1))
tot_ip += 2**(32 - mask)
sheet['D'+str(counter)].value = tot_ip
targ.save(excel_dir + file_name)