Replaced the msg object with a dict

Added configuration file for default values
This commit is contained in:
2004-01-14 21:02:18 +00:00
parent 133e589ef8
commit 757dce470c
2 changed files with 51 additions and 40 deletions

7
TODO
View File

@@ -2,9 +2,6 @@ TODO for dnsupdate
- 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
@@ -12,9 +9,11 @@ TODO for dnsupdate
Done:
- Add a config-file for hostname, domain and password. Option to select
All options should be configurable, ttl and force as well.
- Fix the global variabel mess!!
- Add command line parameter prefixes
-c alternative config file *
-c alternative config file
-d domain
-f force *
-h help

View File

@@ -1,13 +1,7 @@
#! /usr/bin/env python
# dnsupdate.py
# Updates a dynamic dns-record using a TSIG key.
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
# this host connects from
@@ -17,12 +11,15 @@ def get_ipaddress():
ip = f.read().strip()
return ip
def main():
def main(msg):
from optparse import OptionParser
import sys
# Define option parameters
usage = "usage: %prog [-n] hostname [-i] ip adress [OPTIONS]"
parser = OptionParser(usage)
parser.add_option("-c", "--config",
type="string",
help="File containing configuration data")
parser.add_option("-d", "--domain",
type="string",
help="Name of the domain to update")
@@ -43,29 +40,42 @@ def main():
help="TTL in seconds")
(options, args) = parser.parse_args()
if options.ipaddress:
msg.ipaddress = options.ipaddress
else:
msg.ipaddress = get_ipaddress()
if options.domain:
msg.domain = options.domain
if options.keyname:
msg.keyname = options.keyname
if options.password:
msg.keysecret = options.password
if options.ttl:
msg.ttl = options.ttl
if options.config:
msg['cfgfile'] = options.config
msg = readcfg(msg)
if not options.name:
print "A hostname is required for operation.\nEnter '%s -h' for usage information" % sys.argv[0]
return
if options.domain:
msg['domain'] = options.domain
if options.ipaddress:
msg['ipaddress'] = options.ipaddress
else:
msg.hostname = options.name
update()
msg['ipaddress'] = get_ipaddress()
if options.keyname:
msg['keyname'] = options.keyname
if options.name:
msg['hostname'] = options.name
if options.password:
msg['keysecret'] = options.password
if options.ttl:
msg['ttl'] = options.ttl
#print msg
update(msg)
def update():
def readcfg(msg):
# Reads the config file for default info
cfgfile = open(msg['cfgfile'], 'r')
for line in cfgfile.readlines():
(key, value) = line.split('\t', 1)
msg[key] = value.strip()
cfgfile.close()
return msg
def update(msg):
import dns.query
import dns.tsigkeyring
import dns.update
@@ -75,32 +85,34 @@ def update():
# Sanity check incoming data
#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
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({
msg.keyname: msg.keysecret
msg['keyname']: msg['keysecret']
})
# dns.update.Update(name of domain, keyring, keyname)
update = dns.update.Update(msg.domain, keyring=keyring, keyname=msg.keyname)
update = dns.update.Update(msg['domain'], keyring=keyring, keyname=msg['keyname'])
# update.replace(hostname, ttl, record-type, new ip)
update.replace(msg.hostname, msg.ttl, 'a', msg.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" % (msg.hostname, msg.domain, msg.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()
import os.path
msg = {}
msg['cfgfile'] = os.path.expanduser("~/.dnsupdaterc")
main(msg)