query our own IP only once
[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 myip = urllib2.urlopen('https://'+server+'/checkip').read().strip()
11
12 def update_domain(domain):
13         '''Update the given domain, using the global server, user, password. Returns True on success, False on failure.'''
14         global myip
15         # check if the domain is already mapped to our current IP
16         domainip = socket.gethostbyname(domain)
17         if myip == domainip:
18                 # nothing to do
19                 return True
20
21         # we need to update the IP
22         result = urllib2.urlopen('https://'+server+'/update?password='+urllib.quote(password)+'&domain='+urllib.quote(domain)+'&ip='+urllib.quote(myip)).read().strip()
23         if 'good '+myip == result: 
24                 # all went all right
25                 return True
26         else:
27                 # Something went wrong
28                 print "Unexpected answer from server",server,"while updating",domain,"to",myip
29                 print result
30                 return False
31
32 exitcode = 0
33 for domain in domains:
34         if not update_domain(domain):
35                 exitcode = 1
36 sys.exit(exitcode)