allow configuring the port
[dyn-nsupdate.git] / client-scripts / dyn-ns-client
1 #!/usr/bin/python3
2 import urllib.request, socket, sys
3
4 # configuration variables
5 server = 'ns.ralfj.de'
6 domains = ['domain.dyn.ralfj.de'] # list of domains to update
7 password = 'yourpassword'
8 # END of configuration variables
9
10 def urlopen(url):
11         return urllib.request.urlopen(url).read().decode('utf-8').strip()
12
13 myip = urlopen('https://'+server+'/checkip')
14
15 def update_domain(domain):
16         '''Update the given domain, using the global server, user, password. Returns True on success, False on failure.'''
17         global myip
18         # check if the domain is already mapped to our current IP
19         domainip = socket.gethostbyname(domain)
20         if myip == domainip:
21                 # nothing to do
22                 return True
23
24         # we need to update the IP
25         result = urlopen('https://'+server+'/update?password='+urllib.parse.quote(password)+'&domain='+urllib.parse.quote(domain)+'&ip='+urllib.parse.quote(myip))
26         if 'good '+myip == result: 
27                 # all went all right
28                 return True
29         else:
30                 # Something went wrong
31                 print("Unexpected answer from server",server,"while updating",domain,"to",myip)
32                 print(result)
33                 return False
34
35 exitcode = 0
36 for domain in domains:
37         if not update_domain(domain):
38                 exitcode = 1
39 sys.exit(exitcode)