-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog.py
108 lines (94 loc) · 2.89 KB
/
watchdog.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
101
102
103
104
105
106
107
108
#!/bin/env python3
import http.client as httplib
from urllib.request import urlopen
import time, os, datetime
ahora = datetime.datetime.now()
log= "/usr/share/hassio/homeassistant/pyscript/watchdog.csv"
HA='OK'
servidor='OK'
router='OK'
def restart_router():
import pyautogui
import time
pyautogui.FAILSAFE = False
#entra a firefox
pyautogui.click(90, 755)
time.sleep(3)
#ip del router
pyautogui.typewrite('http://192.168.1.1/')
time.sleep(0.2)
pyautogui.press('right')
time.sleep(0.2)
pyautogui.press('enter')
time.sleep(2)
#se entra a pagina del router
pyautogui.press('down')
time.sleep(.5)
pyautogui.press('enter')
time.sleep(0.5)
pyautogui.press('tab', presses=2)
time.sleep(0.5)
pyautogui.press('enter')
time.sleep(2.5)
# va a pagina para reiniciar el router
pyautogui.press('tab', presses=2)
time.sleep(0.2)
pyautogui.press('tab', presses=2)
time.sleep(0.2)
pyautogui.press('tab', presses=2)
time.sleep(0.2)
pyautogui.press('tab')
pyautogui.typewrite('http://192.168.1.1/getpage.gch?pid=1002&nextpage=manager_dev_conf_t.gch')
pyautogui.press('enter')
time.sleep(1)
#activa el reinicio
pyautogui.press('tab')
time.sleep(0.2)
pyautogui.press('tab')
time.sleep(0.2)
pyautogui.press('enter')
time.sleep(0.2)
pyautogui.press('left')
time.sleep(0.2)
pyautogui.press('enter') #print(' -- Se reinicia router')
router='Reiniciado'
servidor='Reiniciado'
with open(log, "a") as o:
o.write(f'{ahora.strftime("%d/%m/%Y")},{ahora.strftime("%H:%M")},{HA},{servidor},{router}\n')
time.sleep(30)
os.system('reboot')
def restart_server():
#print (' --Se reinicia servicio de HA')
os.system('systemctl restart hassio-supervisor.service')
HA='Reiniciado'
time.sleep(10)
try:
urlopen("http://localhost:8123")
#print(' -- HA vuelve a su normalidad!')
except Exception:
#print(' --Intento fallido, se reinicia servidor...')
servidor='Reiniciado'
with open(log, "a") as o:
o.write(f'{ahora.strftime("%d/%m/%Y")},{ahora.strftime("%H:%M")},{HA},{servidor},{router}\n')
time.sleep(10)
os.system('reboot')
def main():
#se revisa conexión a internet
conn = httplib.HTTPSConnection("8.8.8.8", timeout=5)
try:
conn.request("HEAD", "/")
#print(' -- Internet OK')
except Exception:
#print(' -- No internet!')
restart_router()
#se revisa si HA esta funcionando
try:
urlopen("http://localhost:8123")
#print(' -- HA OK')
except Exception:
#print(' -- No HA!')
restart_server()
with open(log, "a") as o:
o.write(f'{ahora.strftime("%d/%m/%Y")},{ahora.strftime("%H:%M")},{HA},{servidor},{router}\n')
if __name__== "__main__" :
main()