Added --delete option to remove host from zone
Added --quiet option to suppress output
This commit is contained in:
1
TODO
1
TODO
@@ -23,6 +23,7 @@ Done:
|
|||||||
-k keyname
|
-k keyname
|
||||||
-n hostname
|
-n hostname
|
||||||
-p password
|
-p password
|
||||||
|
-q quiet
|
||||||
-t ttl
|
-t ttl
|
||||||
- Auto-detect the machine's external ip address'
|
- Auto-detect the machine's external ip address'
|
||||||
Should work on OS X, Linux and Windows.
|
Should work on OS X, Linux and Windows.
|
||||||
|
|||||||
48
dnsupdate.py
48
dnsupdate.py
@@ -2,11 +2,13 @@
|
|||||||
# dnsupdate.py
|
# dnsupdate.py
|
||||||
# Updates a dynamic dns-record using a TSIG key.
|
# Updates a dynamic dns-record using a TSIG key.
|
||||||
|
|
||||||
|
|
||||||
def checkerror(msg):
|
def checkerror(msg):
|
||||||
if len(msg['error']) > 0:
|
if len(msg['error']) > 0:
|
||||||
for error in msg['error']:
|
if not msg.has_key('quiet'):
|
||||||
print "Error: %s" % error
|
for error in msg['error']:
|
||||||
print "Use -h for help"
|
print "Error: %s" % error
|
||||||
|
print "Use -h for help"
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
@@ -35,6 +37,9 @@ def getparams(msg):
|
|||||||
parser.add_option("-c", "--config",
|
parser.add_option("-c", "--config",
|
||||||
type="string",
|
type="string",
|
||||||
help="Alternate config file")
|
help="Alternate config file")
|
||||||
|
parser.add_option("--delete",
|
||||||
|
action="store_true",
|
||||||
|
help="Remove the host from the zone")
|
||||||
parser.add_option("-d", "--domain",
|
parser.add_option("-d", "--domain",
|
||||||
type="string",
|
type="string",
|
||||||
help="Domain to update")
|
help="Domain to update")
|
||||||
@@ -50,6 +55,9 @@ def getparams(msg):
|
|||||||
parser.add_option("-p", "--password",
|
parser.add_option("-p", "--password",
|
||||||
type="string",
|
type="string",
|
||||||
help="TSIG key")
|
help="TSIG key")
|
||||||
|
parser.add_option("-q", "--quiet",
|
||||||
|
action="store_true",
|
||||||
|
help="Quit mode")
|
||||||
parser.add_option("-t", "--ttl",
|
parser.add_option("-t", "--ttl",
|
||||||
type="int",
|
type="int",
|
||||||
help="TTL in seconds")
|
help="TTL in seconds")
|
||||||
@@ -68,6 +76,8 @@ def getparams(msg):
|
|||||||
if ip:
|
if ip:
|
||||||
msg['ipaddress'] = ip
|
msg['ipaddress'] = ip
|
||||||
|
|
||||||
|
if options.delete:
|
||||||
|
msg['delete'] = options.delete
|
||||||
if options.domain:
|
if options.domain:
|
||||||
msg['domain'] = options.domain
|
msg['domain'] = options.domain
|
||||||
if options.keyname:
|
if options.keyname:
|
||||||
@@ -76,9 +86,12 @@ def getparams(msg):
|
|||||||
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:
|
if options.ttl:
|
||||||
msg['ttl'] = options.ttl
|
msg['ttl'] = options.ttl
|
||||||
|
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
|
||||||
@@ -120,8 +133,11 @@ def update(msg):
|
|||||||
# dns.update.Update(name of domain, keyring, keyname)
|
# dns.update.Update(name of domain, keyring, keyname)
|
||||||
update = dns.update.Update(msg['domain'], keyring=keyring, keyname=msg['keyname'])
|
update = dns.update.Update(msg['domain'], keyring=keyring, keyname=msg['keyname'])
|
||||||
|
|
||||||
# update.replace(hostname, ttl, record-type, new ip)
|
if msg.has_key('delete'):
|
||||||
update.replace(msg['hostname'], msg['ttl'], 'a', msg['ipaddress'])
|
update.delete(msg['hostname'])
|
||||||
|
else:
|
||||||
|
# update.replace(hostname, ttl, record-type, new ip)
|
||||||
|
update.replace(msg['hostname'], msg['ttl'], 'a', msg['ipaddress'])
|
||||||
|
|
||||||
# doit, servername
|
# doit, servername
|
||||||
try:
|
try:
|
||||||
@@ -131,10 +147,14 @@ def update(msg):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Verify response
|
# Verify response
|
||||||
if response.rcode() == 0:
|
if not msg.has_key('quiet'):
|
||||||
print "Host '%s.%s' has been added with ip address %s" % (msg['hostname'], msg['domain'], msg['ipaddress'])
|
if response.rcode() == 0:
|
||||||
else:
|
if msg.has_key("delete"):
|
||||||
print "An error has occurred, the server returned:\n%s" % response
|
print "Host '%s.%s' has been deleted" % (msg['hostname'], msg['domain'])
|
||||||
|
else:
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -167,12 +187,15 @@ def verify_ip(msg):
|
|||||||
except:
|
except:
|
||||||
ip = ""
|
ip = ""
|
||||||
|
|
||||||
if ip == msg['ipaddress']:
|
if ip == msg['ipaddress'] and not msg.has_key('delete'):
|
||||||
msg['error'].append("Nameserver already up to date")
|
msg['error'].append("Nameserver already up to date")
|
||||||
|
elif ip == "" and msg.has_key('delete'):
|
||||||
|
msg['error'].append("Nameserver does not recognise the hostname")
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
|
import sys
|
||||||
msg = {}
|
msg = {}
|
||||||
msg['error'] = []
|
msg['error'] = []
|
||||||
|
|
||||||
@@ -183,3 +206,8 @@ if __name__=="__main__":
|
|||||||
if err == 0:
|
if err == 0:
|
||||||
update(msg)
|
update(msg)
|
||||||
checkerror(msg)
|
checkerror(msg)
|
||||||
|
if err == 0:
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user