Added auto detection of external ip address
This commit is contained in:
7
TODO
7
TODO
@@ -3,9 +3,6 @@ TODO for dnsupdate
|
||||
- Check if address needs update before sending data
|
||||
- Add a config-file for hostname, domain and password
|
||||
All options should be configurable, ttl and force as well.
|
||||
- Auto-detect the machine's external ip address'
|
||||
Should work on OS X, Linux and Windows.
|
||||
Should work with multiple interfaces.
|
||||
- Have options to delete a server as well as add/update
|
||||
- Integrate the relevant dnspython modules
|
||||
- Add a GUI
|
||||
@@ -22,3 +19,7 @@ Done:
|
||||
-n hostname
|
||||
-p password
|
||||
-t ttl
|
||||
- Auto-detect the machine's external ip address'
|
||||
Should work on OS X, Linux and Windows.
|
||||
Should work with multiple interfaces.
|
||||
http://www.showmyip.com/simple eller skriva en egen.
|
||||
|
||||
26
dnsupdate.py
26
dnsupdate.py
@@ -6,7 +6,6 @@ import re
|
||||
import string
|
||||
import sys
|
||||
|
||||
|
||||
# dnsupdate.py
|
||||
# Updates a dynamic dns-record using a TSIG key.
|
||||
|
||||
@@ -15,7 +14,17 @@ keyname = "lubcke.se."
|
||||
keysecret = "ZGhNJ05b8ThmHOXhJvkvMw=="
|
||||
ttl = 60
|
||||
|
||||
def get_ipaddress():
|
||||
# Connects to the dns server to determine which ip address
|
||||
# this host connects from
|
||||
import urllib
|
||||
remote = opener = urllib.FancyURLopener({})
|
||||
f = opener.open("http://www.wahlberg.se/~fredrik/ip.php")
|
||||
ip = f.read().strip()
|
||||
return ip
|
||||
|
||||
def main():
|
||||
# Define option parameters
|
||||
usage = "usage: %prog [-n] hostname [-i] ip adress [OPTIONS]"
|
||||
parser = OptionParser(usage)
|
||||
parser.add_option("-d", "--domain",
|
||||
@@ -39,10 +48,14 @@ def main():
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
|
||||
global domain, keyname, keysecret, ttl
|
||||
global domain, ipaddress, keyname, keysecret, ttl
|
||||
|
||||
if options.domain:
|
||||
domain = options.domain
|
||||
if options.ipaddress:
|
||||
ipaddress = options.ipaddress
|
||||
else:
|
||||
ipaddress = get_ipaddress()
|
||||
if options.keyname:
|
||||
keyname = options.keyname
|
||||
if options.password:
|
||||
@@ -50,18 +63,19 @@ def main():
|
||||
if options.ttl:
|
||||
ttl = options.ttl
|
||||
|
||||
if not options.ipaddress or not options.name:
|
||||
print "IP address and hostname are required for operation.\nEnter '%s -h' for usage information" % sys.argv[0]
|
||||
if not options.name:
|
||||
print "A hostname is required for operation.\nEnter '%s -h' for usage information" % sys.argv[0]
|
||||
return
|
||||
else:
|
||||
global hostname, ipaddress
|
||||
ipaddress = options.ipaddress
|
||||
global hostname
|
||||
hostname = options.name
|
||||
|
||||
update()
|
||||
|
||||
|
||||
def update():
|
||||
# The update function connects to the dns server
|
||||
|
||||
# Sanity check incoming data
|
||||
global hostname, ipaddress, ttl
|
||||
hostname = string.replace(hostname, "."+domain, '')
|
||||
|
||||
Reference in New Issue
Block a user