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:
46
dnsupdate.py
46
dnsupdate.py
@@ -3,13 +3,13 @@
|
|||||||
# Updates a dynamic dns-record using a TSIG key.
|
# Updates a dynamic dns-record using a TSIG key.
|
||||||
|
|
||||||
|
|
||||||
def checkerror(msg):
|
def checkerror(msg, show=""):
|
||||||
# Determine if there are any errors reported
|
# Determine if there are any errors reported
|
||||||
if len(msg['error']) > 0:
|
if len(msg['error']) > 0:
|
||||||
if not msg.has_key('quiet'):
|
if show and not msg.has_key('quiet'):
|
||||||
for error in msg['error']:
|
for error in msg['error']:
|
||||||
print "Error: %s" % error
|
print "Error: %s" % error
|
||||||
print "Use -h for help"
|
print "Use -h option for help"
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
@@ -19,11 +19,14 @@ def checkerror(msg):
|
|||||||
def get_ipaddress():
|
def get_ipaddress():
|
||||||
# Connect to a remote 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 re
|
||||||
import urllib
|
import urllib
|
||||||
remote = opener = urllib.FancyURLopener({})
|
remote = opener = urllib.FancyURLopener({})
|
||||||
try:
|
try:
|
||||||
f = opener.open("http://www.wahlberg.se/~fredrik/ip.php")
|
f = opener.open(msg['ipurl'])
|
||||||
ip = f.read().strip()
|
page = f.read()
|
||||||
|
res = re.search('[12]?[0-9]?[0-9](\.[12]?[0-9]?[0-9]){3}', page)
|
||||||
|
ip = res.group()
|
||||||
return ip
|
return ip
|
||||||
except:
|
except:
|
||||||
msg['error'].append("Could not determine ip address automatically,\n use -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",
|
parser.add_option("-k", "--keyname",
|
||||||
type="string",
|
type="string",
|
||||||
help="Name of the TSIG key")
|
help="Name of the TSIG key")
|
||||||
parser.add_option("-n", "--name",
|
parser.add_option("-n", "--hostname",
|
||||||
type="string",
|
type="string",
|
||||||
help="Hostname of local machine")
|
help="Hostname of local machine")
|
||||||
parser.add_option("-p", "--password",
|
|
||||||
type="string",
|
|
||||||
help="TSIG key")
|
|
||||||
parser.add_option("-q", "--quiet",
|
parser.add_option("-q", "--quiet",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Quit mode")
|
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",
|
parser.add_option("-t", "--ttl",
|
||||||
type="int",
|
type="int",
|
||||||
help="TTL in seconds")
|
help="TTL in seconds")
|
||||||
@@ -78,25 +84,24 @@ def getparams(msg):
|
|||||||
base_params = ['delete',
|
base_params = ['delete',
|
||||||
'domain',
|
'domain',
|
||||||
'force',
|
'force',
|
||||||
|
'hostname',
|
||||||
'keyname',
|
'keyname',
|
||||||
|
'keysecret',
|
||||||
'quiet',
|
'quiet',
|
||||||
'ttl']
|
'ttl',
|
||||||
|
'ipurl']
|
||||||
|
|
||||||
for param in base_params:
|
for param in base_params:
|
||||||
if eval('options.' + param):
|
if eval('options.' + param):
|
||||||
msg[param] = 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:
|
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.name:
|
|
||||||
msg['hostname'] = options.name
|
|
||||||
if options.password:
|
|
||||||
msg['keysecret'] = options.password
|
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@@ -117,7 +122,11 @@ def readcfg(msg):
|
|||||||
cfgfile = open(msg['cfgfile'], 'r')
|
cfgfile = open(msg['cfgfile'], 'r')
|
||||||
|
|
||||||
for line in cfgfile.readlines():
|
for line in cfgfile.readlines():
|
||||||
|
line = line.strip()
|
||||||
|
if line.find("#", 0, 1) == 0:
|
||||||
|
continue
|
||||||
(key, value) = line.split('\t', 1)
|
(key, value) = line.split('\t', 1)
|
||||||
|
if not value.strip().lower() == "false":
|
||||||
msg[key] = value.strip()
|
msg[key] = value.strip()
|
||||||
|
|
||||||
cfgfile.close()
|
cfgfile.close()
|
||||||
@@ -149,7 +158,7 @@ def update(msg):
|
|||||||
try:
|
try:
|
||||||
response = dns.query.tcp(update, '217.78.32.198')
|
response = dns.query.tcp(update, '217.78.32.198')
|
||||||
except:
|
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
|
return
|
||||||
|
|
||||||
# Verify response
|
# Verify response
|
||||||
@@ -208,16 +217,19 @@ if __name__=="__main__":
|
|||||||
|
|
||||||
getparams(msg)
|
getparams(msg)
|
||||||
validate(msg)
|
validate(msg)
|
||||||
|
|
||||||
|
err = checkerror(msg)
|
||||||
|
if err == 0:
|
||||||
if not msg.has_key('force'):
|
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)
|
||||||
if err == 0:
|
if err == 0:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
err = checkerror(msg, show="errors")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user