-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.py
65 lines (57 loc) · 1.89 KB
/
ip.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
import requests,time,json,config
def GetIP():
r = requests.get(r'http://jsonip.com')
return r.json()['ip']
def update():
a = requests.get((BASE_LINK_ALL + ZONE_ID + '/dns_records'),headers={
'Authorization': API_TOKEN,
'Content-Type': "application/json"
})
a = a.json()['result']
found = False
for i in a:
if i['name'] == RECORD_NAME:
a = i
found = True
if not found:
print('NO...')
print('Creating...')
b = requests.post((BASE_LINK_ALL + ZONE_ID + '/dns_records'),headers={
'Authorization': API_TOKEN,
'Content-Type': "application/json"
},json={
"type":"A",
"name": RECORD_NAME,
"content": GetIP()
})
if b.json()['success']:
print()
else:
if a['content'] == GetIP():
print('No update needed')
else:
print('updating...')
b = requests.put((BASE_LINK_ALL + ZONE_ID + '/dns_records/' + (a['id'])),headers={
'Authorization': API_TOKEN,
'Content-Type': "application/json"
},json={
"type":"A",
"name": a['name'],
"content": GetIP()
})
if b.json()['success']:
print('IP aggiornato')
if __name__ == "__main__":
if (not hasattr(config,'ZONE_ID') and
not hasattr(config,'BASE_LINK_ALL') and
not hasattr(config,'API_TOKEN') and
not hasattr(config,'RECORD_NAME')):
print("config error",'\n')
print("Must be defined like in the file config.py.example",'\n')
else:
ZONE_ID = config.ZONE_ID
BASE_LINK_ALL = config.BASE_LINK_ALL
API_TOKEN = config.API_TOKEN
RECORD_NAME = config.RECORD_NAME
update()
k=input("Script completed press Enter to exit")