Replaced the msg object with a dict
Added configuration file for default values
This commit is contained in:
7
TODO
7
TODO
@@ -2,9 +2,6 @@ TODO for dnsupdate
|
|||||||
|
|
||||||
- Check if address needs update before sending data and create a force
|
- Check if address needs update before sending data and create a force
|
||||||
update option
|
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
|
- Have options to delete a server as well as add/update
|
||||||
- Integrate the relevant dnspython modules
|
- Integrate the relevant dnspython modules
|
||||||
- Add a GUI
|
- Add a GUI
|
||||||
@@ -12,9 +9,11 @@ TODO for dnsupdate
|
|||||||
|
|
||||||
|
|
||||||
Done:
|
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!!
|
- Fix the global variabel mess!!
|
||||||
- Add command line parameter prefixes
|
- Add command line parameter prefixes
|
||||||
-c alternative config file *
|
-c alternative config file
|
||||||
-d domain
|
-d domain
|
||||||
-f force *
|
-f force *
|
||||||
-h help
|
-h help
|
||||||
|
|||||||
84
dnsupdate.py
84
dnsupdate.py
@@ -1,13 +1,7 @@
|
|||||||
|
#! /usr/bin/env python
|
||||||
# dnsupdate.py
|
# dnsupdate.py
|
||||||
# Updates a dynamic dns-record using a TSIG key.
|
# 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():
|
def get_ipaddress():
|
||||||
# Connects to the dns server to determine which ip address
|
# Connects to the dns server to determine which ip address
|
||||||
# this host connects from
|
# this host connects from
|
||||||
@@ -17,12 +11,15 @@ def get_ipaddress():
|
|||||||
ip = f.read().strip()
|
ip = f.read().strip()
|
||||||
return ip
|
return ip
|
||||||
|
|
||||||
def main():
|
def main(msg):
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
import sys
|
import sys
|
||||||
# Define option parameters
|
# Define option parameters
|
||||||
usage = "usage: %prog [-n] hostname [-i] ip adress [OPTIONS]"
|
usage = "usage: %prog [-n] hostname [-i] ip adress [OPTIONS]"
|
||||||
parser = OptionParser(usage)
|
parser = OptionParser(usage)
|
||||||
|
parser.add_option("-c", "--config",
|
||||||
|
type="string",
|
||||||
|
help="File containing configuration data")
|
||||||
parser.add_option("-d", "--domain",
|
parser.add_option("-d", "--domain",
|
||||||
type="string",
|
type="string",
|
||||||
help="Name of the domain to update")
|
help="Name of the domain to update")
|
||||||
@@ -43,29 +40,42 @@ def main():
|
|||||||
help="TTL in seconds")
|
help="TTL in seconds")
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
if options.ipaddress:
|
if options.config:
|
||||||
msg.ipaddress = options.ipaddress
|
msg['cfgfile'] = options.config
|
||||||
else:
|
|
||||||
msg.ipaddress = get_ipaddress()
|
msg = readcfg(msg)
|
||||||
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 not options.name:
|
if options.domain:
|
||||||
print "A hostname is required for operation.\nEnter '%s -h' for usage information" % sys.argv[0]
|
msg['domain'] = options.domain
|
||||||
return
|
if options.ipaddress:
|
||||||
|
msg['ipaddress'] = options.ipaddress
|
||||||
else:
|
else:
|
||||||
msg.hostname = options.name
|
msg['ipaddress'] = get_ipaddress()
|
||||||
|
if options.keyname:
|
||||||
update()
|
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.query
|
||||||
import dns.tsigkeyring
|
import dns.tsigkeyring
|
||||||
import dns.update
|
import dns.update
|
||||||
@@ -75,32 +85,34 @@ def update():
|
|||||||
|
|
||||||
# Sanity check incoming data
|
# Sanity check incoming data
|
||||||
#global hostname, ipaddress, ttl
|
#global hostname, ipaddress, ttl
|
||||||
msg.hostname = string.replace(msg.hostname, "."+msg.domain, '')
|
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):
|
if not re.search('^[12]?[0-9]?[0-9](\.[12]?[0-9]?[0-9]){3}$', msg['ipaddress']):
|
||||||
print "Invalid ip address '%s'" % msg.ipaddress
|
print "Invalid ip address '%s'" % msg['ipaddress']
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
# The name of the key and the secret
|
# The name of the key and the secret
|
||||||
keyring = dns.tsigkeyring.from_text({
|
keyring = dns.tsigkeyring.from_text({
|
||||||
msg.keyname: msg.keysecret
|
msg['keyname']: msg['keysecret']
|
||||||
})
|
})
|
||||||
|
|
||||||
# dns.update.Update(name of domain, keyring, keyname)
|
# 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(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
|
# doit, servername
|
||||||
response = dns.query.tcp(update, 'nic.wahlberg.se')
|
response = dns.query.tcp(update, 'nic.wahlberg.se')
|
||||||
|
|
||||||
# Verify response
|
# Verify response
|
||||||
if response.rcode() == 0:
|
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:
|
else:
|
||||||
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__":
|
||||||
msg = dnsvalues()
|
import os.path
|
||||||
main()
|
msg = {}
|
||||||
|
msg['cfgfile'] = os.path.expanduser("~/.dnsupdaterc")
|
||||||
|
main(msg)
|
||||||
Reference in New Issue
Block a user