-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.py
executable file
·80 lines (65 loc) · 2.15 KB
/
library.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
#!/usr/bin/python3.7
# file -- library.py --
import logging
import os
import subprocess
import speedtest
def create_dir():
if not os.path.exists("scripts"):
os.makedirs("scripts")
def remove_dir():
if os.path.exists("scripts"):
os.rmdir("scripts")
def remove_gateways(gateways):
filename = "scripts/removeGateways.sh"
script = "#!/bin/bash\n"
for gateway in gateways:
script += f"route del default gw {gateway}" + "\n"
with open(filename, "w+") as text_file:
text_file.write(script)
if os.path.exists(filename):
subprocess.check_call(['chmod', '+x', filename])
out = subprocess.call(filename)
logging.info("Removed gateways")
os.remove(filename)
def add_gateway(gateway, name):
filename = "scripts/addGateway.sh"
script = "#!/bin/bash\n"
script += f"route add default gw {gateway}" + "\n"
with open(filename, "w+") as text_file:
text_file.write(script)
if os.path.exists(filename):
subprocess.check_call(['chmod', '+x', filename])
out = subprocess.call(filename)
if out == 0:
logging.info(f"Added {name} gateway")
os.remove(filename)
else:
logging.error(f"Failed to add {name} gateway")
def remove_gateway(gateway, name):
filename = "scripts/removeGateway.sh"
script = "#!/bin/bash\n"
script += f"route del default gw {gateway}" + "\n"
with open(filename, "w+") as text_file:
text_file.write(script)
if os.path.exists(filename):
subprocess.check_call(['chmod', '+x', filename])
out = subprocess.call(filename)
if out == 0:
logging.info(f"Removed {name} gateway")
os.remove(filename)
else:
logging.error(f"Failed to remove {name} gateway")
def switch_gateway(gateways, gateway, name):
remove_gateways(gateways)
add_gateway(gateway, name)
def speed_test():
s = speedtest.Speedtest()
s.get_best_server()
s.download()
return s.results.dict()
def in_between(now, start, end):
if start <= end:
return start <= now < end
else:
return start <= now or now < end