Added auto detection of external ip address

This commit is contained in:
2004-01-12 12:35:11 +00:00
parent 47510d8548
commit ba566c270d
2 changed files with 24 additions and 9 deletions

View File

@@ -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, '')