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.
29 import urllib.request, socket, sys, argparse
31 # configuration variables
32 domains = ['domain.dyn.ralfj.de'] # list of domains to update
33 password = 'yourpassword'
37 serverIPv4 = 'ipv4.ns.ralfj.de' # Only needed if haveIPv4 is True. This server should NOT have an AAAA record!
38 serverIPv6 = 'ipv6.ns.ralfj.de' # Only needed if haveIPv6 is True. This server should NOT have an A record!
39 server = 'ns.ralfj.de'
40 # END of configuration variables
43 return urllib.request.urlopen(url).read().decode('utf-8').strip()
46 return urlopen('https://'+server+'/checkip')
48 def getCurIP(domain, family):
50 addr = socket.getaddrinfo(domain, None, family=family)
52 except socket.gaierror: # domain not found
55 def getCurIPv4(domain):
56 return getCurIP(domain, socket.AF_INET)
58 def getCurIPv6(domain):
59 return getCurIP(domain, socket.AF_INET6)
61 def update_domain(server, domain, ipv4, ipv6, password, verbose):
62 '''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.'''
63 assert ipv4 is not None or ipv6 is not None
65 # check what the domain is currently mapped to
66 curIPv4 = getCurIPv4(domain)
67 curIPv6 = getCurIPv6(domain)
69 print("Current status of domain {0} is: IPv4 address '{1}', IPv6 address '{2}'".format(domain, curIPv4, curIPv6))
71 # check if there's something to do
72 needUpdate = (ipv4 is not None and curIPv4 != ipv4) or (ipv6 is not None and curIPv6 != ipv6)
75 print("Everything alread up-to-date, nothing to do")
78 # we need to update the IP
79 url = 'https://'+server+'/update?password='+urllib.parse.quote(password)+'&domain='+urllib.parse.quote(domain)
82 url += '&ip='+urllib.parse.quote(ipv4)
85 url += '&ipv6='+urllib.parse.quote(ipv6)
89 # did everything go as planned?
90 if result == expected:
92 print("Successfully updated domain",domain)
96 # Something went wrong
97 print("Unexpected answer from server",server,"while updating",domain)
101 if __name__ == "__main__":
102 # allow overwriting some values on the command-line
103 parser = argparse.ArgumentParser(description='Update a domain managed by a dyn-nsupdate server')
104 parser.add_argument("-p", "--password",
105 dest="password", default=password,
106 help="The password used to update the domains")
107 parser.add_argument("-v", "--verbose",
108 action="store_true", dest="verbose",
109 help="Be more verbose")
110 parser.add_argument("domains", metavar='DOMAIN', nargs='*', default=domains,
111 help="The domains to update")
112 args = parser.parse_args()
116 myIPv4 = getMyIP(serverIPv4)
118 print("My IPv4 is",myIPv4)
123 myIPv6 = getMyIP(serverIPv6)
125 print("My IPv6 is",myIPv6)
129 # update all the domains
131 for domain in args.domains:
132 if not update_domain(server, domain, myIPv4, myIPv6, args.password, verbose=args.verbose):