-
Notifications
You must be signed in to change notification settings - Fork 1
/
ddupdate
executable file
·96 lines (82 loc) · 3.46 KB
/
ddupdate
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
import argparse, functools, configparser, sys
import urllib, urllib.error, urllib.request
parser = argparse.ArgumentParser(description='Update DNS record dynamically')
parser.add_argument('fqdn', type=str, nargs=1, help='DNS record to update')
parser.add_argument('-u', '--url', nargs=1, type=str,
help='URL for the AWS API Gateway to use for updates')
parser.add_argument('-t', '--token', nargs=1, type=str,
help='Security token')
parser.add_argument('-4', '--ipv4', nargs=1, type=str,
help='IPv4 address to advertise (rather than autodetect)')
parser.add_argument('-6', '--ipv6', nargs=1, type=str,
help='IPv6 address to advertise (will not be autodetected)')
parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
help='Display verbose results from the server')
parser.add_argument('-f', '--config', nargs=1, type=str, dest='config',
help='Specify a config file with default options')
parser.add_argument('-l', '--ttl', nargs=1, type=int, dest='ttl',
help='TTL for the injected DNS record')
parser.add_argument('-z', '--zoneid', nargs=1, type=str, dest='zoneid',
help='AWS hosted zone ID string')
args = parser.parse_args()
vargs = vars(args)
if vargs['config'] != None:
conffile = [ vargs['config'] ]
else:
conffile = ['/usr/local/etc/dyndns.conf', '/etc/dyndns.conf']
config = configparser.RawConfigParser()
for f in conffile:
config.read(f)
for host in vargs['fqdn']:
query_args = ['fqdn','token','ipv4','ipv6', 'ttl', 'zoneid']
# fqdn is fixed (it's the host we're iterating on). The rest can come
# from either the command line (vargs) or the config file, or be None
# if no value is provided.
arghash = {'fqdn': host}
for item in ['url'] + query_args[1:]:
if vargs[item] != None:
arghash[item] = vargs[item]
else:
try:
arghash[item] = config.get(host, item)
except Exception as e:
arghash[item] = None
# Since token and url can come from a config file or command line, we
# need to validate we have these for this host.
if arghash['token'] == None:
print("Missing security token for " + host)
sys.exit(1)
if arghash['url'] == None:
print("Missing URL for " + host)
sys.exit(2)
if arghash['zoneid'] == None:
print("Missing hosted zone ID for " + host)
sys.exit(3)
# Collect non-null arguments to be passed via query string, and format
# the query string appropriately.
qstring = functools.reduce(lambda x, y: x + '&' + y, \
list(map(lambda x: x + '=' + str(arghash[x]), \
list(filter(lambda x: arghash[x] != None, query_args)) )) )
try:
u = urllib.request.urlopen(arghash['url'] + '?' + qstring)
data = u.read()
u.close()
except urllib.error.HTTPError as e:
print("HTTP error: " + str(e))
sys.exit(1)
except urllib.error.URLError as e:
print("Network error: " + str(e))
print("URL: " + arghash['url'] + '?' + qstring)
sys.exit(2)
except ValueError as e:
print("Invalid URL: " + args.url[0])
sys.exit(3)
if args.verbose:
print(data)
if u.getcode() == 200:
# Success!
sys.exit(0)
else:
print("Failed to set DNS records.")
sys.exit(1)