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.
25 # The views and conclusions contained in the software and documentation are those
26 # of the authors and should not be interpreted as representing official policies,
27 # either expressed or implied, of the FreeBSD Project.
28 #==============================================================================
29 # configuration variables
30 domains = ['test.dyn.example.com'] # list of domains to update
31 password = 'some_secure_password'
35 serverIPv4 = 'ipv4.ns.example.com' # Only needed if haveIPv4 is True. This server should NOT have an AAAA record!
36 serverIPv6 = 'ipv6.ns.example.com' # Only needed if haveIPv6 is True. This server should NOT have an A record!
37 server = 'ns.example.com'
38 # END of configuration variables
39 #==============================================================================
41 import urllib.request, socket, sys, argparse
44 return urllib.request.urlopen(url).read().decode('utf-8').strip()
47 return urlopen('https://'+server+'/checkip')
49 def getCurIP(domain, family):
51 addr = socket.getaddrinfo(domain, None, family=family)
53 except socket.gaierror: # domain not found
56 def getCurIPv4(domain):
57 return getCurIP(domain, socket.AF_INET)
59 def getCurIPv6(domain):
60 return getCurIP(domain, socket.AF_INET6)
62 def update_domain(server, domain, ipv4, ipv6, password, verbose):
63 '''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.'''
64 assert ipv4 is not None or ipv6 is not None
66 # check what the domain is currently mapped to
67 curIPv4 = getCurIPv4(domain)
68 curIPv6 = getCurIPv6(domain)
70 print("Current status of domain {0} is: IPv4 address '{1}', IPv6 address '{2}'".format(domain, curIPv4, curIPv6))
72 # check if there's something to do
73 needUpdate = (ipv4 is not None and curIPv4 != ipv4) or (ipv6 is not None and curIPv6 != ipv6)
76 print("Everything alread up-to-date, nothing to do")
79 # we need to update the IP
80 url = 'https://'+server+'/update?password='+urllib.parse.quote(password)+'&domain='+urllib.parse.quote(domain)
83 url += '&ip='+urllib.parse.quote(ipv4)
86 url += '&ipv6='+urllib.parse.quote(ipv6)
90 # did everything go as planned?
91 if result == expected:
93 print("Successfully updated domain",domain)
97 # Something went wrong
98 print("Unexpected answer from server",server,"while updating",domain)
102 if __name__ == "__main__":
103 # allow overwriting some values on the command-line
104 parser = argparse.ArgumentParser(description='Update a domain managed by a dyn-nsupdate server')
105 parser.add_argument("-p", "--password",
106 dest="password", default=password,
107 help="The password used to update the domains")
108 parser.add_argument("-v", "--verbose",
109 action="store_true", dest="verbose",
110 help="Be more verbose")
111 parser.add_argument("domains", metavar='DOMAIN', nargs='*', default=domains,
112 help="The domains to update")
113 args = parser.parse_args()
117 myIPv4 = getMyIP(serverIPv4)
119 print("My IPv4 is",myIPv4)
124 myIPv6 = getMyIP(serverIPv6)
126 print("My IPv6 is",myIPv6)
130 # update all the domains
132 for domain in args.domains:
133 if not update_domain(server, domain, myIPv4, myIPv6, args.password, verbose=args.verbose):