-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathip_update.py
More file actions
68 lines (59 loc) · 2.21 KB
/
ip_update.py
File metadata and controls
68 lines (59 loc) · 2.21 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import os.path # lib pour test fichiers
from util_funct import get_json_data_from_file, log_error,log_event
def opendns():
"""
Update opendns with the current global ip address
"""
global debug_print
currentpathdir = os.path.dirname(os.path.realpath(__file__))
cred_file = os.path.join(currentpathdir, "credential.txt")
data_json = get_json_data_from_file(cred_file)
opendns_url = '@updates.dnsomatic.com/nic/update?hostname='
opendns_name = data_json['Token_OPENDNS_NAME']
opendns_password = data_json['Token_OPENDNS_PASSWORD']
opendns_domain = data_json['Token_OPENDNS_DOMAIN']
opendns_wildcard = '&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG'
url = "https://%s:%s%s%s%s" % (opendns_name, opendns_password,opendns_url
, opendns_domain,opendns_wildcard)
r = requests.get(url)
if r.status_code != requests.codes.ok:
log_error("Fail to update OpenDNS %s" % r.text)
exit()
if debug_print:
print("Failed to update OpenDNS.", r.text)
else:
if debug_print:
print("Successfully updated IP:", r.text)
log_event("update OpenDNS ok: %s" % r.text)
def duckdns():
"""
Update duckdns.org with the current global ip address
"""
global debug_print
currentpathdir = os.path.dirname(os.path.realpath(__file__))
cred_file = os.path.join(currentpathdir, "credential.txt")
data_json = get_json_data_from_file(cred_file)
duck_url = 'https://www.duckdns.org/update?domains='
api_token = data_json['Token_DUCK']
url = "%s%s&ip=" % (duck_url, api_token)
r = requests.get(url)
if r.status_code != requests.codes.ok:
log_error("Fail to update DuckDns: %s" % r.text)
if debug_print:
print("Failed to update DuckDns: ", r.text)
else:
log_event("update DuckDNS ok: %s" % r.text)
if debug_print:
print("DuckDns updated: ", r.text)
if __name__ == "__main__":
'''
start this script with cron : sudo crontab -e
for example every hour
0 * * * * python /this_script.py > /dev/null 2>&1
'''
debug_print = True
opendns()
duckdns()