add checkip script and a client to update the IP
authorRalf Jung <post@ralfj.de>
Sat, 10 Aug 2013 11:33:51 +0000 (13:33 +0200)
committerRalf Jung <post@ralfj.de>
Sat, 10 Aug 2013 11:33:51 +0000 (13:33 +0200)
checkip [new file with mode: 0755]
client.py [new file with mode: 0755]
update [moved from update.py with 81% similarity, mode: 0755]

diff --git a/checkip b/checkip
new file mode 100755 (executable)
index 0000000..bc9b5bd
--- /dev/null
+++ b/checkip
@@ -0,0 +1,9 @@
+#!/usr/bin/python
+import os
+
+# print headers
+print "Content-Type: text/plain"
+print ""
+
+# print content
+print os.environ["REMOTE_ADDR"]
diff --git a/client.py b/client.py
new file mode 100755 (executable)
index 0000000..5c1b630
--- /dev/null
+++ b/client.py
@@ -0,0 +1,21 @@
+#!/usr/bin/python
+import urllib2, socket, urllib, sys
+# configuration variables
+user = 'yourusername'
+password = 'yourpassword'
+domain = 'your.domain.ralfj.de'
+# END of configuration variables
+myip = urllib2.urlopen('https://ns.ralfj.de/checkip').read().strip()
+currip = socket.gethostbyname(domain)
+if myip == currip:
+       # nothing to do
+       sys.exit(0)
+
+# we need to update the IP
+result = urllib2.urlopen('https://ns.ralfj.de/update?user='+urllib.quote(user)+'&password='+urllib.quote(password)+'&domain='+urllib.quote(domain)+'&ip='+urllib.quote(myip)).read().strip()
+if 'good '+myip == result: 
+       # nothing to do, all went all right
+       sys.exit(0)
+
+# there was an error :(
+print result
old mode 100644 (file)
new mode 100755 (executable)
similarity index 81%
rename from update.py
rename to update
index 0642c40..1c149be
--- a/update.py
+++ b/update
@@ -1,7 +1,5 @@
 #!/usr/bin/python
 import cgi, os, sys, subprocess
 #!/usr/bin/python
 import cgi, os, sys, subprocess
-import cgitb
-cgitb.enable()
 form = cgi.FieldStorage()
 
 # print headers
 form = cgi.FieldStorage()
 
 # print headers
@@ -10,19 +8,21 @@ print ""
 
 # get input
 if "user" not in form or "password" not in form or "domain" not in form or "ip" not in form:
 
 # get input
 if "user" not in form or "password" not in form or "domain" not in form or "ip" not in form:
-    print "Error:"
     print "Mandatory argument missing: You must supply all of 'user', 'password', 'domain', 'ip'"
     sys.exit()
 
     print "Mandatory argument missing: You must supply all of 'user', 'password', 'domain', 'ip'"
     sys.exit()
 
+ip = form["ip"].value
+domain = form["domain"].value
 user = form["user"].value
 password = form["password"].value
 user = form["user"].value
 password = form["password"].value
-domain = form["domain"].value
-ip = form["ip"].value
 
 # run update program
 p = subprocess.Popen(["/var/lib/named/dyn-nsupdate", user, password, domain, ip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 (stdout, stderr) = p.communicate()
 
 # run update program
 p = subprocess.Popen(["/var/lib/named/dyn-nsupdate", user, password, domain, ip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 (stdout, stderr) = p.communicate()
-if stdout: raise Exception("Unexpected output from dyn-nsupdate")
+if stdout:
+       print "Unexpected stdout from dyn-nsupdate:"
+       print stdout
+       sys.exit()
 
 # check what it did
 if p.returncode == 1:
 
 # check what it did
 if p.returncode == 1:
@@ -32,5 +32,7 @@ if p.returncode == 1:
 else:
        # all right!
        if p.returncode or stderr:
 else:
        # all right!
        if p.returncode or stderr:
-               raise Exception("Unexpected return code or output from dyn-nsupdate")
+               print "Unexpected stderr from dyn-nsupdate:"
+               print stderr
+               sys.exit()
        print "good",ip
        print "good",ip