2 # Copyright (c) 2014, Ralf Jung <post@ralfj.de>
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
8 # 1. Redistributions of source code must retain the above copyright notice, this
9 # list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 #==============================================================================
25 # configuration variables
26 domains = ['test.dyn.example.com'] # list of domains to update
27 password = 'some_secure_password'
31 serverIPv4 = 'ipv4.ns.example.com' # Only needed if haveIPv4 is True. This server should NOT have an AAAA record!
32 serverIPv6 = 'ipv6.ns.example.com' # Only needed if haveIPv6 is True. This server should NOT have an A record!
33 server = 'ns.example.com'
34 # END of configuration variables
35 #==============================================================================
37 import urllib.request, socket, sys, argparse
40 return urllib.request.urlopen(url).read().decode('utf-8').strip()
43 return urlopen('https://'+server+'/checkip')
45 def getCurIP(domain, family):
47 addr = socket.getaddrinfo(domain, None, family=family)
49 except socket.gaierror: # domain not found
52 def getCurIPv4(domain):
53 return getCurIP(domain, socket.AF_INET)
55 def getCurIPv6(domain):
56 return getCurIP(domain, socket.AF_INET6)
58 def update_domain(server, domain, ipv4, ipv6, password, verbose):
59 '''Update the given domain, using the server, password. ipv4 or ipv6 can be None to not update that record. Returns True on success, False on failure.'''
60 assert ipv4 is not None or ipv6 is not None
62 # check what the domain is currently mapped to
63 curIPv4 = getCurIPv4(domain)
64 curIPv6 = getCurIPv6(domain)
66 print("Current status of domain {0} is: IPv4 address '{1}', IPv6 address '{2}'".format(domain, curIPv4, curIPv6))
68 # check if there's something to do
69 needUpdate = (ipv4 is not None and curIPv4 != ipv4) or (ipv6 is not None and curIPv6 != ipv6)
72 print("Everything already up-to-date, nothing to do")
75 # we need to update the IP
76 url = 'https://'+server+'/update?password='+urllib.parse.quote(password)+'&domain='+urllib.parse.quote(domain)
79 url += '&ip='+urllib.parse.quote(ipv4)
82 url += '&ipv6='+urllib.parse.quote(ipv6)
86 # did everything go as planned?
87 if result == expected:
89 print("Successfully updated domain",domain)
93 # Something went wrong
94 print("Unexpected answer from server",server,"while updating",domain)
98 if __name__ == "__main__":
99 # allow overwriting some values on the command-line
100 parser = argparse.ArgumentParser(description='Update a domain managed by a dyn-nsupdate server')
101 parser.add_argument("-p", "--password",
102 dest="password", default=password,
103 help="The password used to update the domains")
104 parser.add_argument("-v", "--verbose",
105 action="store_true", dest="verbose",
106 help="Be more verbose")
107 parser.add_argument("domains", metavar='DOMAIN', nargs='*', default=domains,
108 help="The domains to update")
109 args = parser.parse_args()
113 myIPv4 = getMyIP(serverIPv4)
115 print("My IPv4 is",myIPv4)
120 myIPv6 = getMyIP(serverIPv6)
122 print("My IPv6 is",myIPv6)
126 # update all the domains
128 for domain in args.domains:
129 if not update_domain(server, domain, myIPv4, myIPv6, args.password, verbose=args.verbose):