-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddnsupdate.py
executable file
·54 lines (39 loc) · 1.4 KB
/
ddnsupdate.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
#!/usr/bin/env python3
import datetime
import os
import requests
def validipaddress(ipaddress):
try:
parts = ipaddress.split('.')
return len(parts) == 4 and all(0 <= int(part) < 256 for part in parts)
except ValueError:
return False # one of the 'parts' not convertible to integer
except (AttributeError, TypeError):
return False # not a string
def main():
username = os.environ['DDNS_USERNAME']
password = os.environ['DDNS_PASSWORD']
headers = {
"Content-type": "application/json"
}
domain = "lukeshingles.com"
# hostname, or @ for apex domain
hostname = "home"
print(f"Starting at local time {datetime.datetime.now().isoformat()}")
apiurl = (f"https://domains.google.com/nic/"
f"update?hostname={hostname}.{domain}")
# Google domains will use the requesting IP address by default.
# Alternatively, we can find it this way:
# with requests.get("http://api.ipify.org") as r:
# assert(r.status_code == 200)
# ipaddress = r.text
# assert(validipaddress(ipaddress))
#
# print(f"External IP address is {ipaddress}")
# apiurl += f'&myip={ipaddress}'
with requests.get(apiurl, headers=headers, auth=(username, password)) as r:
print(f'{r.status_code} response from {apiurl}')
print(f' {r.text}')
print("")
if __name__ == "__main__":
main()