-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtech2xl.py
758 lines (585 loc) · 30.5 KB
/
tech2xl.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# tech2xl
#
# Parses a file containing one or more show tech of Cisco devices
# and extracts system information. Then it writes to an Excel file
# You can put show tech of as many Cisco devices as you want in one file
# or you can have multiple files and use wildcards
#
# Requires xlwt library. For Python 3, use xlwt-future (https://pypi.python.org/pypi/xlwt-future)
#
# usage: python tech2xl <Excel output file> <inputfile>...
#
# Author: Andres Gonzelez, dec 2015
import re, glob, sys, csv, collections
import xlwt
import time
def expand(s, list):
for item in list:
if len(s) <= len(item):
if s.lower() == item.lower()[:len(s)]:
return item
return None
def expand_string(s, list):
result = ''
for pos, word in enumerate(s.split()):
expanded_word = expand(word, list[pos])
if expanded_word is not None:
result = result + ' ' + expanded_word
else:
return None
return result[1:]
start_time = time.time()
print("tech2xl v1.5")
if len(sys.argv) < 3:
print("Usage: tech2xl <output file> <input files>...")
sys.exit(2)
commands = [["show"], \
["version", "cdp", "technical-support", "running-config", "interfaces", "diag", "inventory"], \
["neighbors", "status"], \
["detail"]]
int_types = ["Ethernet", "FastEthernet", "GigabitEthernet", "Gigabit", "TenGigabit", "Serial", "ATM", "Port-channel", "Tunnel", "Loopback"]
# Inicialized the collections.OrderedDictionary that will store all the info
systeminfo = collections.OrderedDict()
intinfo = collections.OrderedDict()
cdpinfo = collections.OrderedDict()
diaginfo = collections.OrderedDict()
#These are the fields to be extracted
systemfields = ["Name", "Model", "System ID", "Mother ID", "Image"]
intfields = ["Name", \
"Interface", \
"Type", \
"Number", \
"Description", \
"Status", \
"Line protocol", \
"Hardware", \
"Mac address", \
"Encapsulation", \
"Switchport mode", \
"Access vlan", \
"Voice vlan", \
"IP address", \
"Mask bits", \
"Mask", \
"Network", \
"Input errors", \
"CRC", \
"Frame errors", \
"Overrun", \
"Ignored", \
"Output errors", \
"Collisions", \
"Interface resets", \
"DLCI", \
"Duplex", \
"Speed", \
"Media type"]
cdpfields = ["Name", "Local interface", "Remote device name", "Remote device domain", "Remote interface", "Remote device IP"]
diagfields = ["Name", "Slot", "Subslot", "Description", "Serial number", "Part number"]
masks = ["128.0.0.0","192.0.0.0","224.0.0.0","240.0.0.0","248.0.0.0","252.0.0.0","254.0.0.0","255.0.0.0","255.128.0.0","255.192.0.0","255.224.0.0","255.240.0.0","255.248.0.0","255.252.0.0","255.254.0.0","255.255.0.0","255.255.128.0","255.255.192.0","255.255.224.0","255.255.240.0","255.255.248.0","255.255.252.0","255.255.254.0","255.255.255.0","255.255.255.128","255.255.255.192","255.255.255.224","255.255.255.240","255.255.255.248","255.255.255.252","255.255.255.254","255.255.255.255"]
# takes all arguments starting from 2nd
for arg in sys.argv[2:]:
# uses glob to consider wildcards
for file in glob.glob(arg):
infile = open(file, "r")
# This is the name of the router
name = ''
# Identifies the section of the file that is currently being read
command = ''
section = ''
item = ''
cdp_neighbor = ''
take_next_line = 0
for line in infile:
# checks for device name in prompt
m = re.search("^([a-zA-Z0-9][a-zA-Z0-9_\-\.]*)[#>]\s*([\w\-\_\s\b\a]*)", line)
# avoids a false positive in the "show switch detail" or "show flash: all" section of show tech
if m and not (command == "show switch detail" or command == "show flash: all"):
if name == '':
infile.seek(0)
else:
#removes all deleted chars with backspace (\b) and bell chars (\a)
cli = m.group(2)
while re.search("\b|\a", cli):
cli = re.sub("[^\b]\b|\a", "", cli)
cli = re.sub("^\b", "", cli)
command = expand_string(cli, commands)
name = m.group(1)
section = ''
item = ''
if name not in systeminfo.keys():
systeminfo[name] = collections.OrderedDict(zip(systemfields, [''] * len(systemfields)))
systeminfo[name]['Name'] = name
if name not in intinfo.keys():
intinfo[name] = collections.OrderedDict()
if name not in cdpinfo.keys():
cdpinfo[name] = collections.OrderedDict()
continue
# detects section within show tech
m = re.search("^------------------ (.*) ------------------$", line)
if m:
command = m.group(1)
section = ''
item = ''
continue
# processes "show running-config" command or section of sh tech
if command == 'show running-config':
# extracts information as per patterns
m = re.match("hostname ([a-zA-Z0-9][a-zA-Z0-9_\-\.]*)", line)
if m:
if name == '':
name = m.group(1)
infile.seek(0)
section = ''
item = ''
if name not in systeminfo.keys():
systeminfo[name] = collections.OrderedDict(zip(systemfields, [''] * len(systemfields)))
systeminfo[name]['Name'] = name
if name not in intinfo.keys():
intinfo[name] = collections.OrderedDict()
if name not in cdpinfo.keys():
cdpinfo[name] = collections.OrderedDict()
continue
m = re.match("interface (\S*)", line)
if m:
section = 'interface'
item = m.group(1)
if item not in intinfo[name].keys():
intinfo[name][item] = collections.OrderedDict(zip(intfields, [''] * len(intfields)))
intinfo[name][item]['Name'] = name
intinfo[name][item]['Interface'] = item
intinfo[name][item]['Type'] = re.split('\d', item)[0]
intinfo[name][item]['Number'] = re.split('\D+', item, 1)[1]
continue
if section == 'interface':
if line == '!':
section = ''
continue
m = re.match(" description (.*)", line)
if m:
intinfo[name][item]['Description'] = m.group(1)
continue
m = re.match(" switchport mode (\w*)", line)
if m:
intinfo[name][item]['Switchport mode'] = m.group(1)
continue
m = re.search(" switchport access vlan (\d+)", line)
if m:
intinfo[name][item]["Access vlan"] = m.group(1)
continue
m = re.search(" switchport voice vlan (\d+)", line)
if m:
intinfo[name][item]["Voice vlan"] = m.group(1)
continue
m = re.search(" frame-relay interface-dlci (\d+)", line)
if m:
intinfo[name][item]["DLCI"] = int(m.group(1))
continue
m = re.search("^ ip address ([\d|\.]+) ([\d|\.]+)", line)
if m:
intinfo[name][item]['IP address'] = m.group(1)
intinfo[name][item]['Mask'] = m.group(2)
intinfo[name][item]['Mask bits'] = masks.index(m.group(2)) + 1
m = re.search("(\d+)\.(\d+)\.(\d+)\.(\d+)", intinfo[name][item]['IP address'])
a = int(m.group(1))
b = int(m.group(2))
c = int(m.group(3))
d = int(m.group(4))
m = re.search("(\d+)\.(\d+)\.(\d+)\.(\d+)", intinfo[name][item]['Mask'])
intinfo[name][item]['Network'] = str(a & int(m.group(1))) + '.' + \
str(b & int(m.group(2))) + '.' + \
str(c & int(m.group(3))) + '.' + \
str(d & int(m.group(4)))
continue
# processes "show version" command or section of sh tech
if command == 'show version' and name != '':
# extracts information as per patterns
m = re.search("Processor board ID (.*)", line)
if m:
systeminfo[name]['System ID'] = m.group(1)
continue
m = re.search("Model number\s*: (.*)", line)
if m:
systeminfo[name]['Model'] = m.group(1)
continue
m = re.search("^cisco (.*) processor", line)
if m:
systeminfo[name]['Model'] = m.group(1)
continue
m = re.search("^Cisco (.*) \(revision", line)
if m:
systeminfo[name]['Model'] = m.group(1)
continue
m = re.search("Motherboard serial number\s*: (.*)", line)
if m:
systeminfo[name]['Mother ID'] = m.group(1)
continue
m = re.search('System image file is \"flash:\/?(.*)\.bin\"', line)
if m:
systeminfo[name]['Image'] = m.group(1)
continue
m = re.search('System image file is \"flash:\/.*\/(.*)\.bin\"', line)
if m:
systeminfo[name]['Image'] = m.group(1)
continue
m = re.search('System image file is \"bootflash:(.*)\.bin\"', line)
if m:
systeminfo[name]['Image'] = m.group(1)
continue
m = re.search('System image file is \"sup-bootflash:(.*)\.bin\"', line)
if m:
systeminfo[name]['Image'] = m.group(1)
continue
# processes "show interfaces" command or section of sh tech
if command == 'show interfaces' and name != '':
# extracts information as per patterns
m = re.search("^(\S+) is ([\w|\s]+), line protocol is (\w+)", line)
if m:
item = m.group(1)
if item not in intinfo[name].keys():
intinfo[name][item] = collections.OrderedDict(zip(intfields, [''] * len(intfields)))
intinfo[name][item]['Name'] = name
intinfo[name][item]['Interface'] = item
intinfo[name][item]['Status'] = m.group(2)
intinfo[name][item]['Line protocol'] = m.group(3)
continue
m = re.search("Hardware is (.+), address is ([\w|\.]+)", line)
if m:
intinfo[name][item]['Hardware'] = m.group(1)
intinfo[name][item]['Mac address'] = m.group(2)
continue
m = re.search("Hardware is ([\w\s-]+)$", line)
if m:
intinfo[name][item]['Hardware'] = m.group(1)
continue
m = re.search("^ Encapsulation ([\d|\w|\s|-]+),", line)
if m:
intinfo[name][item]['Encapsulation'] = m.group(1)
continue
m = re.search("^ Description: (.*)", line)
if m:
intinfo[name][item]['Description'] = m.group(1)
continue
m = re.search("^ Internet address is ([\d|\.]+)\/(\d+)", line)
if m:
intinfo[name][item]['IP address'] = m.group(1)
intinfo[name][item]['Mask bits'] = int(m.group(2))
intinfo[name][item]['Mask'] = masks[int(m.group(2)) - 1]
m = re.search("(\d+)\.(\d+)\.(\d+)\.(\d+)", intinfo[name][item]['IP address'])
a = int(m.group(1))
b = int(m.group(2))
c = int(m.group(3))
d = int(m.group(4))
m = re.search("(\d+)\.(\d+)\.(\d+)\.(\d+)", intinfo[name][item]['Mask'])
intinfo[name][item]['Network'] = str(a & int(m.group(1))) + '.' + \
str(b & int(m.group(2))) + '.' + \
str(c & int(m.group(3))) + '.' + \
str(d & int(m.group(4)))
continue
m = re.search("(\d+) input errors", line)
if m:
intinfo[name][item]['Input errors'] = int(m.group(1))
m = re.search("(\d+) CRC", line)
if m:
intinfo[name][item]['CRC'] = int(m.group(1))
m = re.search("(\d+) frame", line)
if m:
intinfo[name][item]['Frame errors'] = int(m.group(1))
m = re.search("(\d+) overrun", line)
if m:
intinfo[name][item]['Overrun'] = int(m.group(1))
m = re.search("(\d+) ignored", line)
if m:
intinfo[name][item]['Ignored'] = int(m.group(1))
continue
m = re.search("(\d+) output errors", line)
if m:
intinfo[name][item]['Output errors'] = int(m.group(1))
m = re.search("(\d+) collisions", line)
if m:
intinfo[name][item]['Collisions'] = int(m.group(1))
m = re.search("(\d+) interface resets", line)
if m:
intinfo[name][item]['Interface resets'] = int(m.group(1))
continue
m = re.search("(\w+) Duplex, (\d+)Mbps, link type is (\w+), media type is (.*)", line)
if m:
intinfo[name][item]['Duplex'] = m.group(3) + "-" + m.group(1)
intinfo[name][item]['Speed'] = m.group(3) + "-" + m.group(2)
intinfo[name][item]['Media type'] = m.group(4)
continue
m = re.search("(\w+)-duplex, (\d+)Mb/s, media type is (.*)", line)
if m:
intinfo[name][item]['Duplex'] = m.group(1)
intinfo[name][item]['Speed'] = m.group(2)
intinfo[name][item]['Media type'] = m.group(3)
continue
# processes "show interfaces status" command or section of sh tech
if command == 'show interfaces status' and name != '':
if (line[:4] != "Port"):
item = expand(line[:2], int_types)
if item is not None:
item = item + line[2:8].rstrip()
if item not in intinfo[name].keys():
intinfo[name][item] = collections.OrderedDict(zip(intfields, [''] * len(intfields)))
intinfo[name][item]['Name'] = name
intinfo[name][item]['Interface'] = item
intinfo[name][item]['Type'] = re.split('\d', item)[0]
intinfo[name][item]['Number'] = re.split('\D+', item, 1)[1]
m = re.search("(.+) (connected|notconnect|disabled)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)", line[8:])
if m:
if not intinfo[name][item].get('Description'):
intinfo[name][item]['Description'] = m.group(1)
if not intinfo[name][item].get('Status'):
intinfo[name][item]['Status'] = m.group(2)
if not intinfo[name][item].get('Access vlan'):
if m.group(3) == 'trunk':
intinfo[name][item]['Switchport mode'] = 'trunk'
elif m.group(3) == 'routed':
intinfo[name][item]['Switchport mode'] = 'routed'
else:
intinfo[name][item]['Access vlan'] = m.group(3)
intinfo[name][item]['Duplex'] = m.group(4)
intinfo[name][item]['Speed'] = m.group(5)
intinfo[name][item]['Media type'] = m.group(6)
# processes "show CDP neighbors" command or section of sh tech
if command == 'show cdp neighbors' and name != '':
# extracts information as per patterns
m = re.search("^([a-zA-Z0-9][a-zA-Z0-9_\-\.]*)$", line)
if m:
if m.group(1) != "Capability" and m.group(1) != "Device":
cdp_neighbor = m.group(1)
continue
m = re.search("^ (...) (\S+)", line)
if m and cdp_neighbor != '':
local_int = expand(m.group(1), int_types) + m.group(2)
remote_int_draft = line[68:-1]
tmp = expand(remote_int_draft[:2], int_types)
if tmp is not None:
remote_int = tmp + remote_int_draft[3:].strip()
else:
remote_int = remote_int_draft
if (name + local_int + remote_int) not in cdpinfo.keys():
cdpinfo[name + local_int + remote_int] = collections.OrderedDict()
if cdp_neighbor not in cdpinfo[name + local_int + remote_int].keys():
cdpinfo[name + local_int + remote_int][cdp_neighbor] = collections.OrderedDict(zip(cdpfields, [''] * len(cdpfields)))
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Name'] = name
#splits name and domain, if any
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote device name'] = cdp_neighbor.split('.',1)[0]
if len(cdp_neighbor.split('.')) > 1:
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote device domain'] = cdp_neighbor.split('.',1)[1]
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Local interface'] = local_int
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote interface'] = remote_int
cdp_neighbor = ''
continue
m = re.search("^([a-zA-Z0-9][a-zA-Z0-9_\-\.]*)\s+(...) ([\d/]+)\s+\d+\s+", line)
if m:
cdp_neighbor = m.group(1)
local_int = expand(m.group(2), int_types) + m.group(3)
remote_int_draft = line[68:-1]
tmp = expand(remote_int_draft[:2], int_types)
if tmp is not None:
remote_int = tmp + remote_int_draft[3:]
else:
remote_int = remote_int_draft
if (name + local_int) not in cdpinfo.keys():
cdpinfo[name + local_int + remote_int] = collections.OrderedDict()
if cdp_neighbor not in cdpinfo[name + local_int + remote_int].keys():
cdpinfo[name + local_int + remote_int][cdp_neighbor] = collections.OrderedDict(zip(cdpfields, [''] * len(cdpfields)))
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Name'] = name
#splits name and domain, if any
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote device name'] = cdp_neighbor.split('.',1)[0]
if len(cdp_neighbor.split('.')) > 1:
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote device domain'] = cdp_neighbor.split('.',1)[1]
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Local interface'] = local_int
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote interface'] = remote_int
cdp_neighbor = ''
# processes "show inventory" command
if command == 'show inventory' and name != '':
# extracts information as per patterns
m = re.search('NAME: .* on Slot (\d+) SubSlot (\d+)\", DESCR: \"(.+)\"', line)
if m:
slot = m.group(1)
subslot = m.group(2)
item = slot + '-' + subslot
if (name + item) not in diaginfo.keys():
diaginfo[name + item] = collections.OrderedDict(zip(diagfields, [''] * len(diagfields)))
diaginfo[name + item]['Name'] = name
diaginfo[name + item]['Slot'] = slot
diaginfo[name + item]['Subslot'] = subslot
diaginfo[name + item]['Description'] = m.group(3)
continue
# extracts information as per patterns
m = re.search('NAME: .* on Slot (\d+)\", DESCR: \"(.+)\"', line)
if m:
slot = m.group(1)
subslot = ''
item = slot
if (name + item) not in diaginfo.keys():
diaginfo[name + item] = collections.OrderedDict(zip(diagfields, [''] * len(diagfields)))
diaginfo[name + item]['Name'] = name
diaginfo[name + item]['Slot'] = slot
diaginfo[name + item]['Subslot'] = subslot
diaginfo[name + item]['Description'] = m.group(2)
continue
m = re.search('PID: (.*)\s*, VID: .*, SN: (\S+)', line)
if m and item != '':
diaginfo[name + item]['Name'] = name
diaginfo[name + item]['Slot'] = slot
diaginfo[name + item]['Subslot'] = subslot
diaginfo[name + item]['Part number'] = m.group(1)
diaginfo[name + item]['Serial number'] = m.group(2)
continue
# processes "show diag" command
if command == 'show diag' and name != '':
# extracts information as per patterns
m = re.search("^(.*) EEPROM:$", line)
if m:
slot = m.group(1)
subslot = ''
item = slot
if (name + item) not in diaginfo.keys():
diaginfo[name + item] = collections.OrderedDict(zip(diagfields, [''] * len(diagfields)))
diaginfo[name + item]['Name'] = name
diaginfo[name + item]['Slot'] = slot
diaginfo[name + item]['Subslot'] = subslot
continue
m = re.search("^Slot (\d+):$", line)
if m:
slot = m.group(1)
subslot = ''
item = slot
if (name + item) not in diaginfo.keys():
diaginfo[name + item] = collections.OrderedDict(zip(diagfields, [''] * len(diagfields)))
diaginfo[name + item]['Name'] = name
diaginfo[name + item]['Slot'] = slot
diaginfo[name + item]['Subslot'] = subslot
take_next_line = 1
continue
# submodules are showed indented from base modules
m = re.search("^\s.*Slot (\d+):$", line)
if m:
subslot = m.group(1)
item = slot + '-' + subslot
if (name + item) not in cdpinfo.keys():
diaginfo[name + item] = collections.OrderedDict(zip(diagfields, [''] * len(diagfields)))
diaginfo[name + item]['Name'] = name
diaginfo[name + item]['Slot'] = slot
diaginfo[name + item]['Subslot'] = subslot
take_next_line = 1
continue
if take_next_line == 1:
diaginfo[name + item]['Description'] = line.strip()
take_next_line = 0
continue
m = re.search("\s+Product \(FRU\) Number\s+: (.+)", line)
if m:
diaginfo[name + item]['Part number'] = m.group(1)
continue
m = re.search("\s+FRU Part Number\s+(.+)", line)
if m:
diaginfo[name + item]['Part number'] = m.group(1)
continue
m = re.search("\s+PCB Serial Number\s+: (.+)", line)
if m:
diaginfo[name + item]['Serial number'] = m.group(1)
continue
m = re.search("\s+Serial number\s+(\S+)", line)
if m:
diaginfo[name + item]['Serial number'] = m.group(1)
continue
# processes "show CDP neighbors detail" command or section of sh tech
if command == 'show cdp neighbors detail' and name != '':
# extracts information as per patterns
m = re.search("^Device ID: ([a-zA-Z0-9][a-zA-Z0-9_\-\.]*)", line)
if m:
cdp_neighbor = m.group(1)
continue
m = re.search("^ IP address: (.*)", line)
if m:
cdp_ip = m.group(1)
continue
m = re.search("Interface: ([\w\/]+), Port ID \(outgoing port\): (.*)", line)
if m:
local_int = m.group(1)
remote_int = m.group(2)
if (name + local_int + remote_int) not in cdpinfo.keys():
cdpinfo[name + local_int + remote_int] = collections.OrderedDict()
if cdp_neighbor not in cdpinfo[name + local_int + remote_int].keys():
cdpinfo[name + local_int + remote_int][cdp_neighbor] = collections.OrderedDict(zip(cdpfields, [''] * len(cdpfields)))
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Name'] = name
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote device'] = cdp_neighbor
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote device name'] = cdp_neighbor.split('.',1)[0]
if len(cdp_neighbor.split('.')) > 1:
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote device domain'] = cdp_neighbor.split('.',1)[1]
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Local interface'] = local_int
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote interface'] = remote_int
cdpinfo[name + local_int + remote_int][cdp_neighbor]['Remote device IP'] = cdp_ip
cdp_neighbor = ''
cdp_ip = ''
continue
# Writes all the information collected
style_header = xlwt.easyxf('font: bold 1')
# Writes system information
cont = len(systeminfo.keys())
print(cont, " devices")
if cont > 0:
wb = xlwt.Workbook()
ws_system = wb.add_sheet('System')
for i, value in enumerate(systemfields):
ws_system.write(0, i, value, style_header)
row = 1
for name in systeminfo.keys():
for col in range(0,len(systemfields)):
ws_system.write(row, col, systeminfo[name][systemfields[col]])
row = row + 1
# Writes interface information
cont = 0
for name in intinfo.keys():
cont = cont + len(intinfo[name])
print(cont, " interfaces")
if cont > 0:
ws_int = wb.add_sheet('Interfaces')
for i, value in enumerate(intfields):
ws_int.write(0, i, value, style_header)
row = 1
for name in intinfo.keys():
for item in intinfo[name].keys():
for col in range(0,len(intfields)):
ws_int.write(row, col, intinfo[name][item][intfields[col]])
row = row + 1
# Writes CDP information
cont = 0
for name in cdpinfo.keys():
cont = cont + len(cdpinfo[name])
print(cont, " neighbors")
if cont > 0:
ws_cdp = wb.add_sheet('CDP neighbors')
for i, value in enumerate(cdpfields):
ws_cdp.write(0, i, value, style_header)
row = 1
for name in cdpinfo.keys():
for item in cdpinfo[name].keys():
for col in range(0,len(cdpfields)):
ws_cdp.write(row, col, cdpinfo[name][item][cdpfields[col]])
row = row + 1
# Writes show diag information
cont = len(diaginfo.keys())
print(cont, " modules")
if cont > 0:
ws_diag = wb.add_sheet('Modules')
for i, value in enumerate(diagfields):
ws_diag.write(0, i, value, style_header)
row = 1
for item in diaginfo.keys():
for col in range(0,len(diagfields)):
ws_diag.write(row, col, diaginfo[item][diagfields[col]])
row = row + 1
try:
wb.save(sys.argv[1])
except IOError as e:
print("Could not write " + sys.argv[1] + ". Check if file is not open in Excel. \nError: ", e)
sys.exit(1)
else:
print("No device found")
print("%s seconds" %(time.time() - start_time))