Fixed embarrasing global variabel mess
This commit is contained in:
12
TODO
12
TODO
@@ -1,8 +1,10 @@
|
|||||||
TODO for dnsupdate
|
TODO for dnsupdate
|
||||||
|
|
||||||
- Check if address needs update before sending data
|
- Check if address needs update before sending data and create a force
|
||||||
- Add a config-file for hostname, domain and password
|
update option
|
||||||
All options should be configurable, ttl and force as well.
|
- Add a config-file for hostname, domain and password. Option to select
|
||||||
|
|
||||||
|
All options should be configurable, ttl and force as well.
|
||||||
- Have options to delete a server as well as add/update
|
- Have options to delete a server as well as add/update
|
||||||
- Integrate the relevant dnspython modules
|
- Integrate the relevant dnspython modules
|
||||||
- Add a GUI
|
- Add a GUI
|
||||||
@@ -10,9 +12,11 @@ TODO for dnsupdate
|
|||||||
|
|
||||||
|
|
||||||
Done:
|
Done:
|
||||||
|
- Fix the global variabel mess!!
|
||||||
- Add command line parameter prefixes
|
- Add command line parameter prefixes
|
||||||
|
-c alternative config file *
|
||||||
-d domain
|
-d domain
|
||||||
-f force
|
-f force *
|
||||||
-h help
|
-h help
|
||||||
-i ipadress
|
-i ipadress
|
||||||
-k keyname
|
-k keyname
|
||||||
|
|||||||
64
dnsupdate.py
64
dnsupdate.py
@@ -1,18 +1,12 @@
|
|||||||
import dns.query
|
|
||||||
import dns.tsigkeyring
|
|
||||||
import dns.update
|
|
||||||
from optparse import OptionParser
|
|
||||||
import re
|
|
||||||
import string
|
|
||||||
import sys
|
|
||||||
|
|
||||||
# dnsupdate.py
|
# dnsupdate.py
|
||||||
# Updates a dynamic dns-record using a TSIG key.
|
# Updates a dynamic dns-record using a TSIG key.
|
||||||
|
|
||||||
domain = "lubcke.se"
|
class dnsvalues:
|
||||||
keyname = "lubcke.se."
|
def __init__(self):
|
||||||
keysecret = "ZGhNJ05b8ThmHOXhJvkvMw=="
|
self.domain = "lubcke.se"
|
||||||
ttl = 60
|
self.keyname = "lubcke.se."
|
||||||
|
self.keysecret = "ZGhNJ05b8ThmHOXhJvkvMw=="
|
||||||
|
self.ttl = 60
|
||||||
|
|
||||||
def get_ipaddress():
|
def get_ipaddress():
|
||||||
# Connects to the dns server to determine which ip address
|
# Connects to the dns server to determine which ip address
|
||||||
@@ -24,6 +18,8 @@ def get_ipaddress():
|
|||||||
return ip
|
return ip
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
from optparse import OptionParser
|
||||||
|
import sys
|
||||||
# Define option parameters
|
# Define option parameters
|
||||||
usage = "usage: %prog [-n] hostname [-i] ip adress [OPTIONS]"
|
usage = "usage: %prog [-n] hostname [-i] ip adress [OPTIONS]"
|
||||||
parser = OptionParser(usage)
|
parser = OptionParser(usage)
|
||||||
@@ -32,7 +28,7 @@ def main():
|
|||||||
help="Name of the domain to update")
|
help="Name of the domain to update")
|
||||||
parser.add_option("-i", "--ipaddress",
|
parser.add_option("-i", "--ipaddress",
|
||||||
type="string",
|
type="string",
|
||||||
help="IP-address of the host")
|
help="IP-address of the host [auto detected]")
|
||||||
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")
|
||||||
@@ -47,62 +43,64 @@ def main():
|
|||||||
help="TTL in seconds")
|
help="TTL in seconds")
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
global domain, ipaddress, keyname, keysecret, ttl
|
|
||||||
|
|
||||||
if options.domain:
|
|
||||||
domain = options.domain
|
|
||||||
if options.ipaddress:
|
if options.ipaddress:
|
||||||
ipaddress = options.ipaddress
|
msg.ipaddress = options.ipaddress
|
||||||
else:
|
else:
|
||||||
ipaddress = get_ipaddress()
|
msg.ipaddress = get_ipaddress()
|
||||||
|
if options.domain:
|
||||||
|
msg.domain = options.domain
|
||||||
if options.keyname:
|
if options.keyname:
|
||||||
keyname = options.keyname
|
msg.keyname = options.keyname
|
||||||
if options.password:
|
if options.password:
|
||||||
keysecret = options.password
|
msg.keysecret = options.password
|
||||||
if options.ttl:
|
if options.ttl:
|
||||||
ttl = options.ttl
|
msg.ttl = options.ttl
|
||||||
|
|
||||||
if not options.name:
|
if not options.name:
|
||||||
print "A hostname is required for operation.\nEnter '%s -h' for usage information" % sys.argv[0]
|
print "A hostname is required for operation.\nEnter '%s -h' for usage information" % sys.argv[0]
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
global hostname
|
msg.hostname = options.name
|
||||||
hostname = options.name
|
|
||||||
|
|
||||||
update()
|
update()
|
||||||
|
|
||||||
|
|
||||||
def update():
|
def update():
|
||||||
|
import dns.query
|
||||||
|
import dns.tsigkeyring
|
||||||
|
import dns.update
|
||||||
|
import re
|
||||||
|
import string
|
||||||
# The update function connects to the dns server
|
# The update function connects to the dns server
|
||||||
|
|
||||||
# Sanity check incoming data
|
# Sanity check incoming data
|
||||||
global hostname, ipaddress, ttl
|
#global hostname, ipaddress, ttl
|
||||||
hostname = string.replace(hostname, "."+domain, '')
|
msg.hostname = string.replace(msg.hostname, "."+msg.domain, '')
|
||||||
if not re.search('^[12]?[0-9]?[0-9](\.[12]?[0-9]?[0-9]){3}$', ipaddress):
|
if not re.search('^[12]?[0-9]?[0-9](\.[12]?[0-9]?[0-9]){3}$', msg.ipaddress):
|
||||||
print "Invalid ip address '%s'" % ipaddress
|
print "Invalid ip address '%s'" % msg.ipaddress
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
# 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({
|
||||||
keyname: keysecret
|
msg.keyname: msg.keysecret
|
||||||
})
|
})
|
||||||
|
|
||||||
# dns.update.Update(name of domain, keyring, keyname)
|
# dns.update.Update(name of domain, keyring, keyname)
|
||||||
update = dns.update.Update(domain, keyring=keyring, keyname=keyname)
|
update = dns.update.Update(msg.domain, keyring=keyring, keyname=msg.keyname)
|
||||||
|
|
||||||
# update.replace(hostname, ttl, record-type, new ip)
|
# update.replace(hostname, ttl, record-type, new ip)
|
||||||
update.replace(hostname, ttl, 'a', ipaddress)
|
update.replace(msg.hostname, msg.ttl, 'a', msg.ipaddress)
|
||||||
|
|
||||||
# doit, servername
|
# doit, servername
|
||||||
response = dns.query.tcp(update, 'nic.wahlberg.se')
|
response = dns.query.tcp(update, 'nic.wahlberg.se')
|
||||||
|
|
||||||
# Verify response
|
# Verify response
|
||||||
if response.rcode() == 0:
|
if response.rcode() == 0:
|
||||||
print "Host '%s.%s' has been added with ip address %s" % (hostname, domain, ipaddress)
|
print "Host '%s.%s' has been added with ip address %s" % (msg.hostname, msg.domain, msg.ipaddress)
|
||||||
else:
|
else:
|
||||||
print "An error has occurred, the server returned:\n%s" % response
|
print "An error has occurred, the server returned:\n%s" % response
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
|
msg = dnsvalues()
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user