Added options using the optparse module

TTL can now be set
Global variables are a mess and must be fixed!
This commit is contained in:
2004-01-11 22:11:13 +00:00
parent 67c32b490d
commit 18077c99ea

View File

@@ -1,28 +1,69 @@
import dns.query import dns.query
import dns.tsigkeyring import dns.tsigkeyring
import dns.update import dns.update
from optparse import OptionParser
import re import re
import string import string
import sys import sys
# dnsupdate.py # dnsupdate.py
# Updates a dynamic dns-record using a TSIG key. # Updates a dynamic dns-record using a TSIG key.
domain = "lubcke.se" domain = "lubcke.se"
keyname = "lubcke.se." keyname = "lubcke.se."
keysecret = "ZGhNJ05b8ThmHOXhJvkvMw==" 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(): 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 # Sanity check incoming data
global hostname, ipaddress, ttl
hostname = string.replace(hostname, "."+domain, '') hostname = string.replace(hostname, "."+domain, '')
if not re.search('^[12]?[0-9]?[0-9](\.[12]?[0-9]?[0-9]){3}$', ipaddress): if not re.search('^[12]?[0-9]?[0-9](\.[12]?[0-9]?[0-9]){3}$', ipaddress):
print "Invalid ip address '%s'" % ipaddress print "Invalid ip address '%s'" % ipaddress
@@ -38,7 +79,7 @@ def update():
update = dns.update.Update(domain, keyring=keyring, keyname=keyname) update = dns.update.Update(domain, keyring=keyring, keyname=keyname)
# update.replace(hostname, ttl, record-type, new ip) # update.replace(hostname, ttl, record-type, new ip)
update.replace(hostname, 60, 'a', ipaddress) update.replace(hostname, ttl, 'a', ipaddress)
# doit, servername # doit, servername
response = dns.query.tcp(update, 'nic.wahlberg.se') 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 print "An error has occurred, the server returned:\n%s" % response
if __name__=="__main__": if __name__=="__main__":
update() main()