-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyndns.py
39 lines (33 loc) · 1.34 KB
/
dyndns.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
import secrets
import sys
import time
import urllib.request
import urllib.error
BREAK_TIME_SEC=60
def ExecuteUpdate():
for target in secrets.domains:
url = 'https://domains.google.com/nic/update?hostname={hostname}&myip={ip}'.format(**target, ip=ip)
request = urllib.request.Request(url)
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
auth_handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
auth_handler.add_password(None, url, target['username'], target['password'])
opener = urllib.request.build_opener(auth_handler)
try:
response = opener.open(request)
print('Success for {hostname}: {response}'.format(**target, response=response.read().decode()))
except urllib.error.HTTPError as e:
print('Failed to update dns for {hostname}: {error}'.format(**target, error=e.msg))
time.sleep(BREAK_TIME_SEC)
sys.stdout.flush()
while True:
try:
ip = urllib.request.urlopen('https://icanhazip.com').read().decode()
print('IP: {ip}'.format(ip=ip))
except urllib.error.HTTPError as e:
print('Failed retrieve external ip: {error}'.format(error=e.msg))
time.sleep(BREAK_TIME_SEC)
continue
try:
ExecuteUpdate()
except:
print('Error during ip update, trying again shortly...')