Added --force option
Minor restructuring
This commit is contained in:
2
TODO
2
TODO
@@ -17,7 +17,7 @@ Done:
|
|||||||
- 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
|
||||||
-i ipadress
|
-i ipadress
|
||||||
-k keyname
|
-k keyname
|
||||||
|
|||||||
44
dnsupdate.py
44
dnsupdate.py
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
|
|
||||||
def checkerror(msg):
|
def checkerror(msg):
|
||||||
|
# Determine if there are any errors reported
|
||||||
if len(msg['error']) > 0:
|
if len(msg['error']) > 0:
|
||||||
if not msg.has_key('quiet'):
|
if not msg.has_key('quiet'):
|
||||||
for error in msg['error']:
|
for error in msg['error']:
|
||||||
@@ -14,8 +15,9 @@ def checkerror(msg):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_ipaddress():
|
def get_ipaddress():
|
||||||
# Connect to the dns server to determine which ip address
|
# Connect to a remote server to determine which ip address
|
||||||
# this host connects from
|
# this host connects from
|
||||||
import urllib
|
import urllib
|
||||||
remote = opener = urllib.FancyURLopener({})
|
remote = opener = urllib.FancyURLopener({})
|
||||||
@@ -29,9 +31,8 @@ def get_ipaddress():
|
|||||||
|
|
||||||
|
|
||||||
def getparams(msg):
|
def getparams(msg):
|
||||||
|
# Read command line parameters and input values
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
import sys
|
|
||||||
# Define option parameters
|
|
||||||
usage = "usage: %prog [OPTIONS]"
|
usage = "usage: %prog [OPTIONS]"
|
||||||
parser = OptionParser(usage)
|
parser = OptionParser(usage)
|
||||||
parser.add_option("-c", "--config",
|
parser.add_option("-c", "--config",
|
||||||
@@ -43,6 +44,9 @@ def getparams(msg):
|
|||||||
parser.add_option("-d", "--domain",
|
parser.add_option("-d", "--domain",
|
||||||
type="string",
|
type="string",
|
||||||
help="Domain to update")
|
help="Domain to update")
|
||||||
|
parser.add_option("--force",
|
||||||
|
action="store_true",
|
||||||
|
help="Force the action")
|
||||||
parser.add_option("-i", "--ipaddress",
|
parser.add_option("-i", "--ipaddress",
|
||||||
type="string",
|
type="string",
|
||||||
help="IP-address of the host [auto detected]")
|
help="IP-address of the host [auto detected]")
|
||||||
@@ -69,36 +73,38 @@ def getparams(msg):
|
|||||||
|
|
||||||
msg = readcfg(msg)
|
msg = readcfg(msg)
|
||||||
|
|
||||||
|
|
||||||
|
# Populate the basic params
|
||||||
|
base_params = ['delete',
|
||||||
|
'domain',
|
||||||
|
'force',
|
||||||
|
'keyname',
|
||||||
|
'quiet',
|
||||||
|
'ttl']
|
||||||
|
|
||||||
|
for param in base_params:
|
||||||
|
if eval('options.' + param):
|
||||||
|
msg[param] = eval('options.' + param)
|
||||||
|
|
||||||
|
# These parameters have other names than their keys, stupid...
|
||||||
if options.ipaddress:
|
if options.ipaddress:
|
||||||
msg['ipaddress'] = options.ipaddress
|
msg['ipaddress'] = options.ipaddress
|
||||||
else:
|
else:
|
||||||
ip = get_ipaddress()
|
ip = get_ipaddress()
|
||||||
if ip:
|
if ip:
|
||||||
msg['ipaddress'] = ip
|
msg['ipaddress'] = ip
|
||||||
|
|
||||||
if options.delete:
|
|
||||||
msg['delete'] = options.delete
|
|
||||||
if options.domain:
|
|
||||||
msg['domain'] = options.domain
|
|
||||||
if options.keyname:
|
|
||||||
msg['keyname'] = options.keyname
|
|
||||||
if options.name:
|
if options.name:
|
||||||
msg['hostname'] = options.name
|
msg['hostname'] = options.name
|
||||||
if options.password:
|
if options.password:
|
||||||
msg['keysecret'] = options.password
|
msg['keysecret'] = options.password
|
||||||
if options.quiet:
|
|
||||||
msg['quiet'] = options.quiet
|
|
||||||
if options.ttl:
|
|
||||||
msg['ttl'] = options.ttl
|
|
||||||
|
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def readcfg(msg):
|
def readcfg(msg):
|
||||||
|
# Read the config file for pre configured values
|
||||||
import os.path
|
import os.path
|
||||||
# Reads the config file for default info
|
|
||||||
if not msg.has_key('cfgfile'):
|
if not msg.has_key('cfgfile'):
|
||||||
if os.path.exists(os.path.expanduser("~/.dnsupdaterc")):
|
if os.path.exists(os.path.expanduser("~/.dnsupdaterc")):
|
||||||
cfgfile = open(os.path.expanduser("~/.dnsupdaterc"), 'r')
|
cfgfile = open(os.path.expanduser("~/.dnsupdaterc"), 'r')
|
||||||
@@ -120,10 +126,10 @@ def readcfg(msg):
|
|||||||
|
|
||||||
|
|
||||||
def update(msg):
|
def update(msg):
|
||||||
|
# The update function connects to the dns server
|
||||||
import dns.query
|
import dns.query
|
||||||
import dns.tsigkeyring
|
import dns.tsigkeyring
|
||||||
import dns.update
|
import dns.update
|
||||||
# The update function connects to the dns server
|
|
||||||
|
|
||||||
# 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({
|
||||||
@@ -179,6 +185,7 @@ def validate(msg):
|
|||||||
|
|
||||||
|
|
||||||
def verify_ip(msg):
|
def verify_ip(msg):
|
||||||
|
# Check if the ip address exists and if it needs an update
|
||||||
import dns.resolver
|
import dns.resolver
|
||||||
try:
|
try:
|
||||||
ans = dns.resolver.query(msg['hostname'] + "." + msg['domain'], 'A')
|
ans = dns.resolver.query(msg['hostname'] + "." + msg['domain'], 'A')
|
||||||
@@ -201,8 +208,11 @@ if __name__=="__main__":
|
|||||||
|
|
||||||
getparams(msg)
|
getparams(msg)
|
||||||
validate(msg)
|
validate(msg)
|
||||||
|
if not msg.has_key('force'):
|
||||||
verify_ip(msg)
|
verify_ip(msg)
|
||||||
|
|
||||||
err = checkerror(msg)
|
err = checkerror(msg)
|
||||||
|
|
||||||
if err == 0:
|
if err == 0:
|
||||||
update(msg)
|
update(msg)
|
||||||
checkerror(msg)
|
checkerror(msg)
|
||||||
|
|||||||
Reference in New Issue
Block a user