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