forked from ccie14023/sparkcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparkcfg.py
100 lines (75 loc) · 2.12 KB
/
sparkcfg.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
import os
import sys
from cli import cli
import time
import difflib
from spark import *
SPARK_ROOM = "Catalyst OnBox"
def save_config():
output = cli('show run')
timestr = time.strftime("%Y%m%d-%H%M%S")
filename = "/bootflash/" + timestr + "_shrun"
f = open(filename,"w")
f.write(output)
f.close
f = open('/bootflash/current_config_name','w')
f.write(filename)
f.close
return filename
def get_cfg_fn():
try:
f = open('/bootflash/current_config_name','r')
except:
return None
fn = f.read()
f.close()
return fn
def compare_configs(cfg1,cfg2):
d = difflib.unified_diff(cfg1, cfg2)
diffstr = ""
for line in d:
if line.find('Current configuration') == -1:
if line.find('Last configuration change') == -1:
if line.find('length 0') == -1:
if line.find('login authentication tacplus') == -1:
if (line.find("+++")==-1) and (line.find("---")==-1):
if (line.find("-!")==-1) and (line.find('+!')==-1):
if line.startswith('+'):
diffstr = diffstr + "\n" + line
elif line.startswith('-'):
diffstr = diffstr + "\n" + line
return diffstr
def do_error(error):
f = open("/bootflash/last_error", "w")
f.write(error)
f.close
if __name__ == '__main__':
timestamp = cli('show clock').strip()
hostname = cli('show run | i hostname').strip()[9:]
user = cli('show log | i SYS-5-CONFIG').split()[-4:-3][0]
old_cfg_fn = get_cfg_fn()
if not old_cfg_fn:
# First time we are running
save_config()
sys.exit()
new_cfg_fn = save_config()
f = open(old_cfg_fn)
old_cfg = f.readlines()
f.close
f = open(new_cfg_fn)
new_cfg = f.readlines()
f.close
os.remove(old_cfg_fn)
d = compare_configs(old_cfg,new_cfg)
if d != "":
text = "**{}** configured by user **{}** at **{}**:".format(hostname, user ,timestamp)
room = get_room_id(SPARK_ROOM, bot_token)
if room == "":
do_error("Unable to get room ID")
if str(post_message(text, room, bot_token).status_code)[0] != '2':
do_error("Post message failed!")
else:
if str(post_message(d, room, bot_token).status_code)[0] != '2':
do_error("Post message failed!")
else:
do_error("No configuration change detected!")