-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmulti_config_stat.py
66 lines (51 loc) · 2.62 KB
/
multi_config_stat.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
# Author : Mehdi CHERIFI
# Note : Please make sure to apply your access_token and room_id in the required emplacement
from genie.testbed import load
from genie.utils.diff import Diff
from genie.libs.parser.utils import get_parser_exclude
import ast
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
testbed=load('connex.yml')
for device in testbed.devices:
testbed.devices[device].connect(log_stdout=False)
# retrieve golden config for each router and transfer it from string dict into python dict
D=open("Golden_Config/golden_conf_"+str(device)+".txt", 'r')
data=D.read()
pre_conf=ast.literal_eval(data)
# retrieve the actual configuration for each router
actual_conf=testbed.devices[device].api.get_running_config_dict()
# apply the diff between the current config and the golden config
diff=Diff(actual_conf, pre_conf)
diff.findDiff()
# save the diff in text file
with open('change_'+str(device)+".txt", 'w') as f:
print(diff, file=f)
f.close()
# open the saved file and verufy if it is empty and there is no space and new line
f=open('change_'+str(device)+".txt", 'r')
content=f.read()
is_empty=content.isspace()
# send message NO CHANGE to Webex room if the diff file is empty and then no change in configuration
if is_empty==True:
access_token = '<YOUR access_token HERE>'
room_id = '<YOUR room_id HERE>'
message = 'NO CHANGE in ' +str(device) +' !!!'
url = 'https://webexapis.com/v1/messages'
headers = {
'Authorization': 'Bearer {}'.format(access_token),
'Content-Type': 'application/json'
}
params = {'roomId': room_id, 'markdown':message}
res = requests.post(url, headers=headers, json=params)
# send the diff text file to Webex room to indicate the change in the configuration
else:
m = MultipartEncoder({'roomId': '<YOUR room_id HERE>',
'text': 'HERE THE CHANGE in '+ str(device) + ' !!!, WE WILL PROCEED FOR GOLDEN CONFIGURATION RESTORING',
'files': ('change_'+str(device)+".txt", open('change_'+str(device)+".txt", 'rb'),
'document/txt')})
r = requests.post('https://webexapis.com/v1/messages', data=m,
headers={'Authorization': 'Bearer <YOUR access_token HERE>',
'Content-Type': m.content_type})
# process for retrieving the golden config saved in disk0 file in the router in case of change
testbed.devices[device].execute("configure replace disk0:startup-config force")