add CGI script which calls dyn-nsupdate
[dyn-nsupdate.git] / update.py
1 #!/usr/bin/python
2 import cgi, os, sys, subprocess
3 import cgitb
4 cgitb.enable()
5 form = cgi.FieldStorage()
6
7 # print headers
8 print "Content-Type: text/plain"
9 print ""
10
11 # get input
12 if "user" not in form or "password" not in form or "domain" not in form or "ip" not in form:
13     print "Error:"
14     print "Mandatory argument missing: You must supply all of 'user', 'password', 'domain', 'ip'"
15     sys.exit()
16
17 ip = form["ip"].value
18 domain = form["domain"].value
19 user = form["user"].value
20 password = form["password"].value
21
22 # run update program
23 p = subprocess.Popen(["/var/lib/named/dyn-nsupdate", user, password, domain, ip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
24 (stdout, stderr) = p.communicate()
25 if stdout: raise Exception("Unexpected output from dyn-nsupdate")
26
27 # check what it did
28 if p.returncode == 1:
29         # error :/
30         print "There was an error while updating the DNS:"
31         print stderr
32 else:
33         # all right!
34         if p.returncode or stderr:
35                 raise Exception("Unexpected return code or output from dyn-nsupdate")
36         print "good",ip