51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
import dns.query
|
|
import dns.tsigkeyring
|
|
import dns.update
|
|
import re
|
|
import string
|
|
import sys
|
|
|
|
domain = "lubcke.se"
|
|
keyname = "lubcke.se."
|
|
keysecret = "ZGhNJ05b8ThmHOXhJvkvMw=="
|
|
|
|
# Updates a dns-record
|
|
|
|
def update():
|
|
# Get command line args
|
|
if len(sys.argv) != 3:
|
|
print "Usage: dnsupdate.py hostname 192.168.0.1"
|
|
return
|
|
|
|
hostname = sys.argv[1]
|
|
ipaddress = sys.argv[2]
|
|
|
|
# Sanity check incoming data
|
|
hostname = string.replace(hostname, "."+domain, '')
|
|
if not re.search('^[12]?[0-9]?[0-9](\.[12]?[0-9]?[0-9]){3}$', ipaddress):
|
|
print "Invalid ip address '%s'" % ipaddress
|
|
return
|
|
|
|
|
|
# The name of the key and the secret
|
|
keyring = dns.tsigkeyring.from_text({
|
|
keyname: keysecret
|
|
})
|
|
|
|
# dns.update.Update(name of domain, keyring, keyname)
|
|
update = dns.update.Update(domain, keyring=keyring, keyname=keyname)
|
|
|
|
# update.replace(hostname, ttl, record-type, new ip)
|
|
update.replace(hostname, 60, 'a', ipaddress)
|
|
|
|
# doit, servername
|
|
response = dns.query.tcp(update, 'nic.wahlberg.se')
|
|
|
|
# Verify response
|
|
if response.rcode() == 0:
|
|
print "Host '%s.%s' has been added with ip address %s" % (hostname, domain, ipaddress)
|
|
else:
|
|
print "An error has occurred, the server returned:\n%s" % response
|
|
|
|
if __name__=="__main__":
|
|
update() |