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