def delete_record(record, server, key_file, key_name): if determine_if_ip_address(record): reverse_record = str(dns.reversename.from_address(record)) re_record = re.search(r"([0-9]+)\.(.*).$", reverse_record) else: re_record = re.search(r"(\w+)\.(.*)$", record) record = re_record.group(1) domain = re_record.group(2) try: key_ring = keyutils.read_tsigkey(key_file, key_name) except: raise update = dns.update.Update(domain, keyring = key_ring) update.delete(record) response = dns.query.tcp(update, server) print "Record Deletion Output: %s\n" % response
def add_reverse_record(fqdn, domain, ip_address, ttl, dns_server, key_file, key_name): """Add a reverse PTR record to the specific DNS server and zone.""" reverse_fqdn = str(dns.reversename.from_address(ip_address)) reverse_ip = re.search(r"([0-9]+).(.*).$", reverse_fqdn).group(1) reverse_domain = re.search(r"([0-9]+).(.*).$", reverse_fqdn).group(2) try: key_ring = keyutils.read_tsigkey(key_file, key_name) except: raise update = dns.update.Update(reverse_domain, keyring = key_ring) update.replace(reverse_ip, ttl, 'PTR', fqdn + ".") try: response = dns.query.tcp(update, dns_server) except Exception, err: exc_type, exc_obj, exc_tb = sys.exc_info() print "type: %s, obj: %s, tb: %s" % (exc_type, exc_obj, exc_tb) print "Unhandled exception in add_forward_record: %s" % err raise
def add_forward_record(hostname, domain, ip_address, ttl, dns_server, key_file, key_name): """Add an A record to the specific DNS server with the provided hostname/IP/key information.""" try: key_ring = keyutils.read_tsigkey(key_file, key_name) except: raise update = dns.update.Update(domain, keyring = key_ring) update.replace(hostname, ttl, 'A', ip_address) try: response = dns.query.tcp(update, dns_server) except dns.tsig.PeerBadKey: print "The remote DNS server %s did not accept the key passed." % dns_server raise except Exception, err: exc_type, exc_obj, exc_tb = sys.exc_info() print "type: %s, obj: %s, tb: %s" % (exc_type, exc_obj, exc_tb) print "Unhandled exception in add_forward_record: %s" % err raise
type="string") parser.add_option("--keyfile", dest="key_file", help="File containing the TSIG key.", type="string") parser.add_option("--keyname", dest="key_name", help="TSIG key name to use for the DDNS update.", type="string") parser.add_option("--server", dest="dns_server", help="DNS server to query.", type="string") parser.add_option("--ttl",dest="ttl", help="Time to Live (in Seconds). Default: 86400", type="int",default=86400) (options, args) = parser.parse_args() orig_hostname = re.search(r"(\w+).(.*)", options.orig).group(1) orig_domain = re.search(r"(\w+).(.*)", options.orig).group(2) print "CNAME record to be added: %s, CNAME points to: %s, DNS Server: %s" % (options.orig, options.dest, options.dns_server) keyring = keyutils.read_tsigkey(options.key_file, options.key_name) update = dns.update.Update(orig_domain, keyring = keyring) update.replace(orig_hostname, options.ttl, 'CNAME', options.dest + ".") print "--- Updating CNAME Record" response = dns.query.tcp(update,options.dns_server) print "Output: %s" % response