forked from jkrauska/rancid-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-parser.py
More file actions
executable file
·70 lines (50 loc) · 1.64 KB
/
config-parser.py
File metadata and controls
executable file
·70 lines (50 loc) · 1.64 KB
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
#!/usr/bin/python
'''
Simple Cisco Switch Config Parser
Can be used with rancid configuration collectors.
Aggregates self-similar port configurations to identify irregularities.
'''
import fileinput
import re
descriptionLine=re.compile('^ description')
commentLine=re.compile('^!')
interfaceLine=re.compile('^interface')
interfaceConfigs=False
settingCollect = {}
settingPortCollect = {}
for line in fileinput.input():
line=line.rstrip()
if interfaceLine.match(line):
interfaceConfigs = True
interfaceName=line.replace('interface ','').replace('GigabitEthernet','Gi')
interfaceSettings = []
#print '-'*80
elif commentLine.match(line) and interfaceConfigs:
interfaceConfigs = False
#print 'Settings', interfaceSettings
foo="\n".join(interfaceSettings)
try:
settingCollect[foo] += 1
settingPortCollect[foo] = settingPortCollect[foo] + ', ' + interfaceName
except:
settingCollect[foo] = 1
settingPortCollect[foo] = interfaceName
if interfaceConfigs and \
not interfaceLine.match(line) and \
not descriptionLine.match(line):
#print interfaceName, line
interfaceSettings.append(line)
# After Parsing the File
for setting in settingCollect.keys():
#print '='*80
#print 's'
#print setting
#print 'sc'
#print settingCollect[setting]
#print 'spc'
#print settingPortCollect[setting]
print '-' * 80
print 'Config pattern:'
print setting
print 'Count:', len(settingPortCollect[setting].split(','))
print 'Port List:', settingPortCollect[setting]