-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-conf.py
222 lines (213 loc) · 7.33 KB
/
parse-conf.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import xlwt
from xlwt import Workbook
from ciscoconfparse import CiscoConfParse
from pathlib import Path
import re
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
path = filedialog.askopenfilename()
print ("File: {}".format(path))
# NXOS running config file
try:
parse = CiscoConfParse(path, syntax='nxos')
except UnicodeDecodeError as uni:
print ("USE A TEXT FILE!!! Closing...")
exit()
wb = Workbook()
# VLANs information page
sheet1 = wb.add_sheet('VLANs')
row1 = 0
sheet1.write(row1, 0, "VLAN")
sheet1.write(row1, 1, "NAME")
for p_obj in parse.find_objects('^vlan')[1:]:
row1 += 1
sheet1.write(row1, 0, str(p_obj)[str(p_obj).find("vlan") + 4:-2])
for c_obj in p_obj.children:
sheet1.write(
row1,
1,
str(c_obj)[
str(c_obj).find("name") +
5:str(c_obj).find("' (parent")])
# VLANs Interfaces information page
sheet2 = wb.add_sheet('SVIs')
row1 = 0
sheet2.write(row1, 0, "SVI")
sheet2.write(row1, 1, "Description")
sheet2.write(row1, 2, "IP")
sheet2.write(row1, 3, "VIP")
sheet2.write(row1, 4, "VRF")
for p_obj in parse.find_objects('^interface Vlan')[1:]:
row1 += 1
sheet2.write(
row1,
0,
str(p_obj)[
str(p_obj).find("interface Vlan") +
14:-
2])
for c_obj in p_obj.children:
if "description" in str(c_obj):
sheet2.write(
row1,
1,
str(c_obj)[
str(c_obj).find("description") +
12:str(c_obj).find("' (parent")])
if "ip address" in str(c_obj):
sheet2.write(
row1,
2,
str(c_obj)[
str(c_obj).find("ip address") +
11:str(c_obj).find("' (parent")])
if "hsrp" in str(c_obj):
for h_obj in c_obj.children:
if "ip" in str(h_obj):
sheet2.write(
row1,
3,
str(h_obj)[
str(h_obj).find("ip") +
3:str(h_obj).find("' (parent")])
if "vrf member" in str(c_obj):
sheet2.write(
row1,
4,
str(c_obj)[
str(c_obj).find("vrf member") +
11:str(c_obj).find("' (parent")])
# Interfaces information page
sheet3 = wb.add_sheet('Ints')
row1 = 0
sheet3.write(row1, 0, "Interface")
sheet3.write(row1, 1, "Description")
sheet3.write(row1, 2, "Type")
sheet3.write(row1, 3, "VLANs/IP")
sheet3.write(row1, 4, "Po")
sheet3.write(row1, 5, "Status")
sheet3.write(row1, 6, "VRF")
for p_obj in parse.find_objects('^interface Ethernet')[1:]:
row1 += 1
sheet3.write(row1, 0, str(p_obj)[str(p_obj).find("interface") + 10:-2])
for c_obj in p_obj.children:
if "description" in str(c_obj):
sheet3.write(
row1,
1,
str(c_obj)[
str(c_obj).find("description") +
12:str(c_obj).find("' (parent")])
try:
if "access vlan" in str(c_obj):
sheet3.write(
row1,
3,
str(c_obj)[
str(c_obj).find("access vlan") +
12:str(c_obj).find("' (parent")])
sheet3.write(row1, 2, "swichport")
if "trunk allowed vlan" in str(c_obj):
sheet3.write(
row1,
3,
str(c_obj)[
str(c_obj).find("trunk allowed vlan") +
19:str(c_obj).find("' (parent")])
sheet3.write(row1, 2, "swichport")
if "ip address" in str(c_obj):
sheet3.write(
row1,
3,
str(c_obj)[
str(c_obj).find("ip address") +
11:str(c_obj).find("' (parent")])
sheet3.write(row1, 2, "routed")
except Exception as ex:
print("Problem with line ", str(p_obj), ex)
sheet3.write(row1, 7, "incoherence in col: {}".format(
int(str(ex)[str(ex).find("colx=") + 5:]) + 1))
if "channel-group" in str(c_obj):
sheet3.write(
row1,
4,
str(c_obj)[
str(c_obj).find("channel-group") +
14:str(c_obj).find("' (parent")])
if "no shutdown" in str(c_obj):
sheet3.write(row1, 5, "no shutdown")
if "' shutdown' (" in str(c_obj):
sheet3.write(row1, 5, "no shutdown")
if "vrf member" in str(c_obj):
sheet3.write(
row1,
6,
str(c_obj)[
str(c_obj).find("vrf member") +
11:str(c_obj).find("' (parent")])
# Port-channels information page
sheet4 = wb.add_sheet('Po')
row1 = 0
sheet4.write(row1, 0, "Interface")
sheet4.write(row1, 1, "Description")
sheet4.write(row1, 2, "Type")
sheet4.write(row1, 3, "VLANs/IP")
sheet4.write(row1, 4, "Status")
sheet4.write(row1, 5, "VRF")
for p_obj in parse.find_objects('^interface port-channel')[1:]:
row1 += 1
sheet4.write(row1, 0, str(p_obj)[str(p_obj).find("interface") + 10:-2])
for c_obj in p_obj.children:
if "description" in str(c_obj):
sheet4.write(
row1,
1,
str(c_obj)[
str(c_obj).find("description") +
12:str(c_obj).find("' (parent")])
try:
if "access vlan" in str(c_obj):
sheet4.write(
row1,
3,
str(c_obj)[
str(c_obj).find("access vlan") +
12:str(c_obj).find("' (parent")])
sheet4.write(row1, 2, "swichport")
if "trunk allowed vlan" in str(c_obj):
sheet4.write(
row1,
3,
str(c_obj)[
str(c_obj).find("trunk allowed vlan") +
19:str(c_obj).find("' (parent")])
sheet4.write(row1, 2, "swichport")
if "ip address" in str(c_obj):
sheet4.write(
row1,
3,
str(c_obj)[
str(c_obj).find("ip address") +
11:str(c_obj).find("' (parent")])
sheet4.write(row1, 2, "routed")
except Exception as ex:
print("Problem with line ", str(p_obj), ex)
sheet4.write(row1, 7, "incoherence in col: {}".format(
int(str(ex)[str(ex).find("colx=") + 5:]) + 1))
if "no shutdown" in str(c_obj):
sheet4.write(row1, 4, "no shutdown")
if "' shutdown' (" in str(c_obj):
sheet4.write(row1, 4, "shutdown")
if "vrf member" in str(c_obj):
sheet4.write(
row1,
5,
str(c_obj)[
str(c_obj).find("vrf member") +
11:str(c_obj).find("' (parent")])
# Save to file
wbname = path.split("/")[-1] + ".xls"
print("The file was generated: {}".format(wbname))
wb.save(path + ".xls")