-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepup.py
84 lines (71 loc) · 2.59 KB
/
keepup.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
import asyncio
import aiohttp
api_endpoint = "my.aeza.net/api"
vm_endpoint = "vm.aeza.net"
minutes = 1 # delay between checks
ip = "1.1.1.1" # server ip
vmmanager_id = "" # server id from vm.aeza.net
aeza_id = "" # server id from my.aeza.net
login = "" # login/email from my.aeza.net
password = "" # password from my.aeza.net
async def ping(ip):
async with aiohttp.ClientSession() as session:
try:
async with session.get("http://" + ip, timeout=1) as resp:
print(resp.status)
return True
except aiohttp.client_exceptions.ServerDisconnectedError as e:
print(repr(e))
print("Server offline")
return False
except asyncio.exceptions.TimeoutError as e:
print(repr(e))
print("Server offline")
return False
async def reboot():
async with aiohttp.ClientSession() as session:
headers = {"authorization": "Bearer undefined"}
json = {"method": "credentials", "email": login, "password": password}
async with session.post(
f"https://{api_endpoint}/auth?", headers=headers, json=json
) as resp:
json_ = await resp.json()
api_key = json_["data"]["session"]
headers = {"authorization": f"Bearer {api_key}"}
async with session.get(
f"https://{api_endpoint}/services/{aeza_id}/goto?", headers=headers
) as resp:
json_ = await resp.json()
keyvm = json_["data"].split("key/")[1]
json = {"key": keyvm}
async with session.post(
f"https://{vm_endpoint}/auth/v3/auth_by_key", json=json
) as resp:
json_ = await resp.json()
sesskey = json_["session"]
real_keyvm = json_["token"]
cookies = {"token": real_keyvm, "ses6": sesskey}
headers = {"x-xsrf-token": real_keyvm}
async with session.post(
f"https://{vm_endpoint}/vm/v3/host/{vmmanager_id}/start",
cookies=cookies,
headers=headers,
) as resp:
json_ = await resp.json()
if "id" in json_:
print("Succes vm started !")
else:
print(f"Error! JSON-response: {json_}")
async def main(): # reboot server if ping error 5 times in a row
error_count = 0
while True:
for i in range(5):
if await ping(ip):
error_count = 0
else:
error_count += 1
if error_count == 5:
await reboot()
break
await asyncio.sleep(60 * minutes)
asyncio.run(main())