migrate server scripts to python 3
[dyn-nsupdate.git] / server-scripts / update
index 5fc519b3530bbed36303202e0d6e5f95dc82deb2..48c791caa2388fba70a8de087f45b48dd9cc6d1e 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 # Copyright (c) 2014, Ralf Jung <post@ralfj.de>
 # All rights reserved.
 # 
 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-# 
-# The views and conclusions contained in the software and documentation are those
-# of the authors and should not be interpreted as representing official policies, 
-# either expressed or implied, of the FreeBSD Project.
 
 import cgi, os, sys, subprocess
 form = cgi.FieldStorage(keep_blank_values=True)
 
 # print headers
-print "Content-Type: text/plain"
-print ""
+print("Content-Type: text/plain")
+print("")
 
 # get input
 if "password" not in form or "domain" not in form or ("ip" not in form and "ipv6" not in form):
-    print "Mandatory argument missing: You must supply all of 'password', 'domain' and at least either 'ip' or 'ipv6'"
+    print("Mandatory argument missing: You must supply all of 'password', 'domain' and at least either 'ip' or 'ipv6'")
     sys.exit()
 
 domain = form.getfirst("domain")
 password = form.getfirst("password")
 ip = form.getfirst("ip")
 ipv6 = form.getfirst("ipv6")
+assert domain is not None and password is not None
+assert ip is not None or ipv6 is not None
 
 # run update program
 args = ["/var/lib/bind/dyn-nsupdate", "--domain", domain, "--password", password]
@@ -55,8 +53,13 @@ p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 # check what it did
 if p.returncode or stderr or stdout:
     # error :/
-    print "There was an error while updating the DNS: Return code %d" % p.returncode
-    if stdout: print stdout
-    if stderr: print stderr
+    print("There was an error while updating the DNS: Return code %d" % p.returncode)
+    if stdout: print(stdout)
+    if stderr: print(stderr)
 else:
-    print "good",ip
+    out = "good"
+    if ip is not None:
+        out += " "+ip
+    if ipv6 is not None:
+        out += " "+ipv6
+    print(out)