118 lines
3.4 KiB
Python
118 lines
3.4 KiB
Python
#! /usr/bin/env python
|
|
# dnsupdate.py
|
|
# Updates a dynamic dns-record using a TSIG key.
|
|
|
|
def get_ipaddress():
|
|
# Connects to the dns server to determine which ip address
|
|
# this host connects from
|
|
import urllib
|
|
remote = opener = urllib.FancyURLopener({})
|
|
f = opener.open("http://www.wahlberg.se/~fredrik/ip.php")
|
|
ip = f.read().strip()
|
|
return ip
|
|
|
|
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")
|
|
parser.add_option("-i", "--ipaddress",
|
|
type="string",
|
|
help="IP-address of the host [auto detected]")
|
|
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()
|
|
|
|
if options.config:
|
|
msg['cfgfile'] = options.config
|
|
|
|
msg = readcfg(msg)
|
|
|
|
if options.domain:
|
|
msg['domain'] = options.domain
|
|
if options.ipaddress:
|
|
msg['ipaddress'] = options.ipaddress
|
|
else:
|
|
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 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
|
|
import re
|
|
import string
|
|
# The update function connects to the dns server
|
|
|
|
# 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']
|
|
return
|
|
|
|
|
|
# The name of the key and the secret
|
|
keyring = dns.tsigkeyring.from_text({
|
|
msg['keyname']: msg['keysecret']
|
|
})
|
|
|
|
# dns.update.Update(name of domain, keyring, 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'])
|
|
|
|
# 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'])
|
|
else:
|
|
print "An error has occurred, the server returned:\n%s" % response
|
|
|
|
if __name__=="__main__":
|
|
import os.path
|
|
msg = {}
|
|
msg['cfgfile'] = os.path.expanduser("~/.dnsupdaterc")
|
|
main(msg) |