-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestart.py
77 lines (55 loc) · 1.94 KB
/
restart.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
import requests
from bs4 import BeautifulSoup
import itertools
import netifaces
import time
from datetime import datetime
def connection_up():
r = requests.get('google.com')
return int(r.status_code) == 200
def get_page(tab):
r = requests.get('http://192.168.100.1/' + tab)
return r.text
def build_dictionary(html):
soup = BeautifulSoup(html)
table = soup.find_all(['td'])
information = [str(item)[4:-5] for item in table]
return dict(itertools.izip_longest(*[iter(information)] * 2, fillvalue=''))
def reset_modem():
r = requests.get('http://192.168.1.100/reset.htm')
def get_filename():
return 'incident-{}.html'.format(datetime.now().strftime("%Y%m%d-%H%M%S"))
while True:
time.sleep(60)
for _ in range(3):
if connection_up:
break
time.sleep(10)
else:
# Get diagnostic information about local host and from Modem
nic = netifaces.ifaddresses('eth0')
nic[netifaces.AF_INET]
try:
status_html = get_page('indexData.htm')
status = build_dictionary(status_html)
address_html = get_page('cmAddressData.htm')
address = build_dictionary(address_html)
signal_html = get_page('cmSignalData.htm')
logs_html = get_page('cmLogsData.htm')
except:
# Modem power off or unreachable
break
# Decide whether or not to restart the Modem
if status['Cable Modem Status'] == 'Operational':
reset_modem()
with open(get_filename(), 'a') as f:
f.write(status_html)
f.write(address_html)
f.write(signal_html)
f.write(logs_html)
time.sleep(120)
if connection_up():
f.write('Connection restored after restart')
else:
f.write('Connection still down after retart')
time.sleep(1800)