From 73b49fde7204c81e26158d9834dee0f7721b02e4 Mon Sep 17 00:00:00 2001 From: Philipp Spitzer Date: Tue, 8 Apr 2014 22:36:13 +0200 Subject: [PATCH] Now the returncode of nsupdate is evaluated. --- bin/nsupdate_dyndns | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/bin/nsupdate_dyndns b/bin/nsupdate_dyndns index e49c08f..237c788 100755 --- a/bin/nsupdate_dyndns +++ b/bin/nsupdate_dyndns @@ -1,10 +1,16 @@ #!/usr/bin/python +import sys import re import argparse from subprocess import Popen, PIPE import ipaddr +class NsupdateError(Exception): + def __init__(self, returncode): + self.returncode = returncode + + def ipfamily_by_ip(ip): if isinstance(ip, ipaddr.IPv4Address): return 'A' @@ -16,31 +22,41 @@ def ipfamily_by_ip(ip): def nsupdate_add(fqdn, ttl, ip): """ :param fqdn: Fully qualified domain name - :param ip_family: A or AAAA""" + :param ip_family: A or AAAA + :raises an NsupdateError in case of errors.""" command = "update add {fqdn} {ttl} IN {ip_family} {ip}\n\n".format(fqdn=fqdn, ttl=ttl, ip_family=ipfamily_by_ip(ip), ip=ip) p = Popen(['nsupdate', '-l'], stdin=PIPE) p.communicate(command) + if p.returncode != 0: + raise NsupdateError(p.returncode) def nsupdate_delete(fqdn, ip_family): """ :param fqdn: Fully qualified domain name - :param ip_family: A or AAAA""" + :param ip_family: A or AAAA + :raises an NsupdateError in case of errors.""" command = "update delete {fqdn} {ip_family}\n\n".format(fqdn=fqdn, ip_family=ip_family) p = Popen(['nsupdate', '-l'], stdin=PIPE) p.communicate(command) + if p.returncode != 0: + raise NsupdateError(p.returncode) def main(args): - if args.delete: - if args.ip is None: - nsupdate_delete(args.fqdn, 'A') - nsupdate_delete(args.fqdn, 'AAAA') + try: + if args.delete: + if args.ip is None: + nsupdate_delete(args.fqdn, 'A') + nsupdate_delete(args.fqdn, 'AAAA') + else: + nsupdate_delete(args.fqdn, ipfamily_by_ip(args.ip)) else: nsupdate_delete(args.fqdn, ipfamily_by_ip(args.ip)) - else: - nsupdate_delete(args.fqdn, ipfamily_by_ip(args.ip)) - nsupdate_add(args.fqdn, args.ttl, args.ip) + nsupdate_add(args.fqdn, args.ttl, args.ip) + except NsupdateError as e: + sys.exit(e.returncode) + if __name__ == '__main__': -- 2.39.5