Renamed a name and password to hostname and keysecret
Added -u option to refer to a webpage where the machines ip can be verified Enabled comments in the config file Minor error message changes
This commit is contained in:
62
dnsupdate.py
62
dnsupdate.py
@@ -3,13 +3,13 @@
|
||||
# Updates a dynamic dns-record using a TSIG key.
|
||||
|
||||
|
||||
def checkerror(msg):
|
||||
def checkerror(msg, show=""):
|
||||
# Determine if there are any errors reported
|
||||
if len(msg['error']) > 0:
|
||||
if not msg.has_key('quiet'):
|
||||
if show and not msg.has_key('quiet'):
|
||||
for error in msg['error']:
|
||||
print "Error: %s" % error
|
||||
print "Use -h for help"
|
||||
print "Use -h option for help"
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
@@ -19,14 +19,17 @@ def checkerror(msg):
|
||||
def get_ipaddress():
|
||||
# Connect to a remote server to determine which ip address
|
||||
# this host connects from
|
||||
import re
|
||||
import urllib
|
||||
remote = opener = urllib.FancyURLopener({})
|
||||
try:
|
||||
f = opener.open("http://www.wahlberg.se/~fredrik/ip.php")
|
||||
ip = f.read().strip()
|
||||
f = opener.open(msg['ipurl'])
|
||||
page = f.read()
|
||||
res = re.search('[12]?[0-9]?[0-9](\.[12]?[0-9]?[0-9]){3}', page)
|
||||
ip = res.group()
|
||||
return ip
|
||||
except:
|
||||
msg['error'].append("Could not determine ip address automatically,\nuse -i switch to enter manually")
|
||||
msg['error'].append("Could not determine ip address automatically,\n use -i switch to enter manually")
|
||||
|
||||
|
||||
|
||||
@@ -53,15 +56,18 @@ def getparams(msg):
|
||||
parser.add_option("-k", "--keyname",
|
||||
type="string",
|
||||
help="Name of the TSIG key")
|
||||
parser.add_option("-n", "--name",
|
||||
parser.add_option("-n", "--hostname",
|
||||
type="string",
|
||||
help="Hostname of local machine")
|
||||
parser.add_option("-p", "--password",
|
||||
type="string",
|
||||
help="TSIG key")
|
||||
parser.add_option("-q", "--quiet",
|
||||
action="store_true",
|
||||
help="Quit mode")
|
||||
parser.add_option("-s", "--keysecret",
|
||||
type="string",
|
||||
help="TSIG key")
|
||||
parser.add_option("-u", "--ipurl",
|
||||
type="string",
|
||||
help="URL to ip server")
|
||||
parser.add_option("-t", "--ttl",
|
||||
type="int",
|
||||
help="TTL in seconds")
|
||||
@@ -76,27 +82,26 @@ def getparams(msg):
|
||||
|
||||
# Populate the basic params
|
||||
base_params = ['delete',
|
||||
'domain',
|
||||
'force',
|
||||
'keyname',
|
||||
'quiet',
|
||||
'ttl']
|
||||
'domain',
|
||||
'force',
|
||||
'hostname',
|
||||
'keyname',
|
||||
'keysecret',
|
||||
'quiet',
|
||||
'ttl',
|
||||
'ipurl']
|
||||
|
||||
for param in base_params:
|
||||
if eval('options.' + param):
|
||||
msg[param] = eval('options.' + param)
|
||||
|
||||
# These parameters have other names than their keys, stupid...
|
||||
# These parameters needs special handling
|
||||
if options.ipaddress:
|
||||
msg['ipaddress'] = options.ipaddress
|
||||
else:
|
||||
ip = get_ipaddress()
|
||||
if ip:
|
||||
msg['ipaddress'] = ip
|
||||
if options.name:
|
||||
msg['hostname'] = options.name
|
||||
if options.password:
|
||||
msg['keysecret'] = options.password
|
||||
|
||||
return msg
|
||||
|
||||
@@ -117,8 +122,12 @@ def readcfg(msg):
|
||||
cfgfile = open(msg['cfgfile'], 'r')
|
||||
|
||||
for line in cfgfile.readlines():
|
||||
line = line.strip()
|
||||
if line.find("#", 0, 1) == 0:
|
||||
continue
|
||||
(key, value) = line.split('\t', 1)
|
||||
msg[key] = value.strip()
|
||||
if not value.strip().lower() == "false":
|
||||
msg[key] = value.strip()
|
||||
|
||||
cfgfile.close()
|
||||
return msg
|
||||
@@ -149,7 +158,7 @@ def update(msg):
|
||||
try:
|
||||
response = dns.query.tcp(update, '217.78.32.198')
|
||||
except:
|
||||
msg['error'].append("An error has occurred, check your keyname and password.")
|
||||
msg['error'].append("An error has occurred, check your keyname and keysecret.")
|
||||
return
|
||||
|
||||
# Verify response
|
||||
@@ -208,16 +217,19 @@ if __name__=="__main__":
|
||||
|
||||
getparams(msg)
|
||||
validate(msg)
|
||||
if not msg.has_key('force'):
|
||||
verify_ip(msg)
|
||||
|
||||
err = checkerror(msg)
|
||||
if err == 0:
|
||||
if not msg.has_key('force'):
|
||||
verify_ip(msg)
|
||||
|
||||
err = checkerror(msg)
|
||||
|
||||
if err == 0:
|
||||
update(msg)
|
||||
checkerror(msg)
|
||||
if err == 0:
|
||||
sys.exit(0)
|
||||
|
||||
err = checkerror(msg, show="errors")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user