diff --git a/dnsupdate.py b/dnsupdate.py index 3242bb2..679ee23 100644 --- a/dnsupdate.py +++ b/dnsupdate.py @@ -1,28 +1,69 @@ import dns.query import dns.tsigkeyring import dns.update +from optparse import OptionParser import re import string import sys + # dnsupdate.py # Updates a dynamic dns-record using a TSIG key. domain = "lubcke.se" keyname = "lubcke.se." keysecret = "ZGhNJ05b8ThmHOXhJvkvMw==" +ttl = 60 + +def main(): + usage = "usage: %prog [-n] hostname [-i] ip adress [OPTIONS]" + parser = OptionParser(usage) + parser.add_option("-d", "--domain", + type="string", + help="Name of the domain to update") + parser.add_option("-i", "--ipaddress", + type="string", + help="IP-address of the host") + parser.add_option("-k", "--keyname", + type="string", + help="Name of the TSIG key") + parser.add_option("-n", "--name", + type="string", + help="Name of the host to update") + parser.add_option("-p", "--password", + type="string", + help="The TSIG key") + parser.add_option("-t", "--ttl", + type="int", + help="TTL in seconds") + (options, args) = parser.parse_args() + + + global domain, keyname, keysecret, ttl + + if options.domain: + domain = options.domain + if options.keyname: + keyname = options.keyname + if options.password: + keysecret = options.password + if options.ttl: + ttl = options.ttl + + if not options.ipaddress or not options.name: + print "IP address and hostname are required for operation.\nEnter '%s -h' for usage information" % sys.argv[0] + return + else: + global hostname, ipaddress + ipaddress = options.ipaddress + hostname = options.name + + update() 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 + global hostname, ipaddress, ttl 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 @@ -38,7 +79,7 @@ def update(): update = dns.update.Update(domain, keyring=keyring, keyname=keyname) # update.replace(hostname, ttl, record-type, new ip) - update.replace(hostname, 60, 'a', ipaddress) + update.replace(hostname, ttl, 'a', ipaddress) # doit, servername response = dns.query.tcp(update, 'nic.wahlberg.se') @@ -50,4 +91,4 @@ def update(): print "An error has occurred, the server returned:\n%s" % response if __name__=="__main__": - update() \ No newline at end of file + main() \ No newline at end of file