diff --git a/TODO b/TODO index 224801d..ae2c67f 100644 --- a/TODO +++ b/TODO @@ -1,8 +1,10 @@ TODO for dnsupdate -- Check if address needs update before sending data -- Add a config-file for hostname, domain and password - All options should be configurable, ttl and force as well. +- Check if address needs update before sending data and create a force + update option +- Add a config-file for hostname, domain and password. Option to select + + All options should be configurable, ttl and force as well. - Have options to delete a server as well as add/update - Integrate the relevant dnspython modules - Add a GUI @@ -10,9 +12,11 @@ TODO for dnsupdate Done: +- Fix the global variabel mess!! - Add command line parameter prefixes + -c alternative config file * -d domain - -f force + -f force * -h help -i ipadress -k keyname diff --git a/dnsupdate.py b/dnsupdate.py index b1ec94e..2c4f7e9 100644 --- a/dnsupdate.py +++ b/dnsupdate.py @@ -1,18 +1,12 @@ -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 +class dnsvalues: + def __init__(self): + self.domain = "lubcke.se" + self.keyname = "lubcke.se." + self.keysecret = "ZGhNJ05b8ThmHOXhJvkvMw==" + self.ttl = 60 def get_ipaddress(): # Connects to the dns server to determine which ip address @@ -24,6 +18,8 @@ def get_ipaddress(): return ip def main(): + from optparse import OptionParser + import sys # Define option parameters usage = "usage: %prog [-n] hostname [-i] ip adress [OPTIONS]" parser = OptionParser(usage) @@ -32,7 +28,7 @@ def main(): help="Name of the domain to update") parser.add_option("-i", "--ipaddress", type="string", - help="IP-address of the host") + help="IP-address of the host [auto detected]") parser.add_option("-k", "--keyname", type="string", help="Name of the TSIG key") @@ -47,62 +43,64 @@ def main(): help="TTL in seconds") (options, args) = parser.parse_args() - - global domain, ipaddress, keyname, keysecret, ttl - - if options.domain: - domain = options.domain if options.ipaddress: - ipaddress = options.ipaddress + msg.ipaddress = options.ipaddress else: - ipaddress = get_ipaddress() + msg.ipaddress = get_ipaddress() + if options.domain: + msg.domain = options.domain if options.keyname: - keyname = options.keyname + msg.keyname = options.keyname if options.password: - keysecret = options.password + msg.keysecret = options.password if options.ttl: - ttl = options.ttl + msg.ttl = options.ttl if not options.name: print "A hostname is required for operation.\nEnter '%s -h' for usage information" % sys.argv[0] return else: - global hostname - hostname = options.name + msg.hostname = options.name update() def update(): + import dns.query + import dns.tsigkeyring + import dns.update + import re + import string # The update function connects to the dns server # 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 + #global hostname, ipaddress, ttl + msg.hostname = string.replace(msg.hostname, "."+msg.domain, '') + if not re.search('^[12]?[0-9]?[0-9](\.[12]?[0-9]?[0-9]){3}$', msg.ipaddress): + print "Invalid ip address '%s'" % msg.ipaddress return # The name of the key and the secret keyring = dns.tsigkeyring.from_text({ - keyname: keysecret + msg.keyname: msg.keysecret }) # dns.update.Update(name of domain, keyring, keyname) - update = dns.update.Update(domain, keyring=keyring, keyname=keyname) + update = dns.update.Update(msg.domain, keyring=keyring, keyname=msg.keyname) # update.replace(hostname, ttl, record-type, new ip) - update.replace(hostname, ttl, 'a', ipaddress) + update.replace(msg.hostname, msg.ttl, 'a', msg.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) + print "Host '%s.%s' has been added with ip address %s" % (msg.hostname, msg.domain, msg.ipaddress) else: print "An error has occurred, the server returned:\n%s" % response if __name__=="__main__": + msg = dnsvalues() main() \ No newline at end of file