-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (40 loc) · 1.19 KB
/
main.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
import json
import paramiko
from requests import get
from typing import Optional
class Config:
def __init__(self, host: str, port: str, username: str, password: str) -> None:
self.host = host
self.port = int(port)
self.username = username
self.password = password
def getConfig() -> Config:
f = open('./config.json')
data = json.load(f)
return Config(data['host'], data['port'], data['username'], data['password'])
def getRouterIp(client: paramiko.SSHClient) -> Optional[str]:
ip = ''
try:
_, stdout, stderr = client.exec_command(
"ifconfig ppp0 | head -n 2 | tail -n 1"
)
res = str(stdout.readline())
ip = res.replace(" ", "").split(" ")[1].replace('addr:', '')
except:
ip = None
return ip
def getPublicIp() -> str:
return get('https://api.ipify.org').content.decode('utf8')
if __name__ == '__main__':
config = getConfig()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=config.host,
username=config.username,
password=config.password,
port=config.port
)
if getRouterIp(ssh) != getPublicIp():
ssh.exec_command("restart")
ssh.close()