forked from extremenetworks/ExtremeScripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf_diff.py
More file actions
128 lines (88 loc) · 3.65 KB
/
conf_diff.py
File metadata and controls
128 lines (88 loc) · 3.65 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
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
from exsh import clicmd
from difflib import Differ
from time import time
import argparse
class ArgParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
self.print_help()
sys.exit(2)
def main():
parser = ArgParser(prog='conf_diff.py',
description='Compare the saved configuration with the currently running one.')
parser.add_argument('-d', '--debug',
help='Enables verbose logging and leaves temp files for debugging',
action='store_true')
parser.add_argument('-c', '--clean',
help='Remove previous debug files. Executes "rm temp_*; rm *.temp"',
action='store_true')
args = parser.parse_args()
debug = args.debug
clean = args.clean
if clean:
print "DEBUG: Cleaning up previous debug files"
clicmd("rm temp_*")
clicmd("rm *.temp")
print "Comparing configurations, please wait..."
#create unique names for temp files
t_stamp = str(time())
if debug:
print "DEBUG: Creating temp files"
saved_name = '/usr/local/cfg/saved_{0}.temp'.format(t_stamp[:-3])
running_name = '/usr/local/cfg/running_{0}.temp'.format(t_stamp[:-3])
temp_config = 'temp_{0}'.format(t_stamp[:-3])
saved_file = open(saved_name, 'w')
running_file = open(running_name, 'w')
# find the selected config file
if debug:
print "DEBUG: Finding selected config"
output = clicmd('show switch | include "Config Selected"', True).split()
selected_config = output[2]
selected_config = selected_config[:-4]
# save the running config to a temp file,
# then convert both config files from XML to human-readable format
if debug:
print "DEBUG: Generating temp version of running config"
clicmd("save config {0}".format(temp_config))
if debug:
print "DEBUG: Generating temp cfgmgr version of selected config"
saved_file.write(clicmd("debug cfgmgr show configuration file {0}".format(selected_config), True))
if debug:
print "DEBUG: Generating temp cfgmgr version of running config"
running_file.write(clicmd("debug cfgmgr show configuration file {0}".format(temp_config), True))
# set the selected config back, since the save config changed it
clicmd("use config {0}".format(selected_config), False)
# close the files, and reopen them for reading
saved_file.close()
running_file.close()
saved_file = open(saved_name, 'r')
running_file = open(running_name, 'r')
# diff the two configs
if debug:
print "DEBUG: Diffing configs"
d = Differ()
diff = list(d.compare(saved_file.readlines(), running_file.readlines()))
# print the results of the diff
print " "
print "If line starts with \'+\', the command has been added since last save."
print "If line starts with \'-\', the command was present in the last save, and has been deleted."
print " "
print "Config changes:"
for line in diff:
if line.startswith('+ ') or line.startswith('- '):
print line[:-1]
print "Note that this script has cleared the CLI dirty bit. The configuration has not been saved."
# clean up
saved_file.close()
running_file.close()
# remove files that were opened
if not debug:
clicmd("rm {0}".format(saved_name))
clicmd("rm {0}".format(running_name))
clicmd("rm {0}.cfg".format(temp_config))
if __name__ == '__main__':
try:
main()
except SystemExit:
# catch SystemExit to prevent EXOS shell from exiting to login prompt
pass