-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraphical_check_access.py
71 lines (56 loc) · 2.13 KB
/
graphical_check_access.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
import time
import atexit
from flask import Flask, render_template
from genie.testbed import load
import shutil
from diagrams.generic.os import Ubuntu
from diagrams import Diagram, Cluster
from diagrams.custom import Custom
from pytz import timezone
import logging
from apscheduler.schedulers.background import BackgroundScheduler
testbed=load('connex.yml')
app = Flask(__name__)
@app.route('/')
# In case Auto Check Access
def result():
return render_template('index_auto_refresh_state.html')
'''
# In case of On-Demand Check Access
def result():
return render_template('index_on-demand_state.html')
'''
def Check_Access(testbed):
#Save the decision for further use
Device_Decision={}
#Try access to each device
for device in testbed.devices:
try:
testbed.devices[device].connect(log_stdout=False)
Device_Decision[str(device)]=testbed.devices[device].type
testbed.devices[device].disconnect()
except Exception as e:
Device_Decision[str(device)]=testbed.devices[device].type+'-no-access'
#Topology Devices Access
Topology_Devices=[]
with Diagram("Topology Access Result", show=False, filename="Topology_Result"):
PC = Ubuntu("Admin")
with Cluster("Network Entreprise"):
SWITCH=Custom("vSwitch", "./static/vSwitch.png")
for k,l in Device_Decision.items():
Dev=Custom(k+'\n'+ testbed.devices[k].connections.cli.protocol+":"+str(testbed.devices[k].connections.cli.port), "./static/"+l+".jpg")
Topology_Devices.append(Dev)
PC >> SWITCH >> Topology_Devices
shutil.move("Topology_Result.png", "static/Topology_Result.png")
return Device_Decision
# Configure the scheduling process of the Check_Access function
scheduler = BackgroundScheduler()
scheduler.add_job(func=Check_Access, trigger="interval", seconds=180, args=[testbed], timezone='Europe/London')
scheduler.start()
# Deactivate the logs in Flask server except the ERROR level
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())
if __name__=='__main__':
app.run()