My ISP gives my server a dynamic IP address which is allocated using DHCP. If the ISP decided to reallocate this IP address for some reason then nobody would be able to access my server because only my ISP would know the new IP address.
DynDNS provides a DNS service that allows dynamic IP addresses to be assigned to domain names. A client program running on the server can check to see if the IP address has been reassigned and, if so, tell the dyndns service which will in turn tell the rest of the world. The service is free (for up to 5 domains) and very reliable.
There are client programs available to update the DynDNS information but I didn't want to use any of these because:
Most are windows
Most linux ones are overcomplex and underdocumented (not unusual)
None support interrogating my DLink DI624 router for the current IP address. They mostly support accessing web sites to get the IP address but this limits how often I can check for changes without being accused of net abuse.
Hence I wrote a python script to do it. This is executed by cron once an hour.
My ISP gives my server a dynamic IP address which is allocated using DHCP. If the ISP decided to reallocate this IP address for some reason then nobody would be able to access my server because only my ISP would know the new IP address.
DynDNS provides a DNS service that allows dynamic IP addresses to be assigned to domain names. A client program running on the server can check to see if the IP address has been reassigned and, if so, tell the dyndns service which will in turn tell the rest of the world. The service is free (for up to 5 domains) and very reliable.
There are client programs available to update the DynDNS information but I didn't want to use any of these because:
Most are windows
Most linux ones are overcomplex and underdocumented (not unusual)
None support interrogating my DLink DI624 router for the current IP address. They mostly support accessing web sites to get the IP address but this limits how often I can check for changes without being accused of net abuse.
Hence I wrote a python script to do it. This is executed by cron once an hour.
#!/usr/bin/python
import urllib2
import string
import base64
import re
import sys
import time
#
# Read config settings.
#
try:
from dynconfig import *
except:
strCurrentAddr = None
strLastUpdate = 0
strRouterName = <CENSORED>
strRouterPassword = <CENSORED>
#
# Send request to dlink DI-624 for status page
# Add authorisation header to log in.
#
oReq = urllib2.Request('http://192.168.0.1/st_devic.html')
oReq.add_header("USER-AGENT", "Mozilla/4.76 en (X11; U; Linux 2.4.1-0.1.9 i586)")
oReq.add_header("AUTHORIZATION", string.strip("Basic " +
base64.encodestring("%s:%s" % (strRouterName, strRouterPassword))))
#
# Search through status page for 'WAN' followed by the IP address
#
bWan = False
for strLine in urllib2.urlopen(oReq):
# print strLine:-1
#
# Searching for the 'WAN' section.
#
if bWan == False:
if strLine.find( 'WAN') >= 0:
bWan = True
else:
#
# Searching for an ip address.
#
oMatch = re.search( r'\d+\.\d+\.\d+\.\d+', strLine)
if oMatch:
strAddr = oMatch.group()
break
else:
if bWan == True:
raise "IP address not found"
else:
raise "WAN section not found"
#
# Create set of urls and login details
#
if 1:
#
# My urls to update.
#
strUrls = <CENSORED>
strName = <CENSORED>
strPassword = <CENSORED>
else:
#
# Testing: test urls to update
#
strUrls = ( "test.homeip.net", "test.mine.nu")
strName = "test"
strPassword = "test"
strAddr = "1.2.3.4"
#
# If address is unchanged do not submit it.
#
if strAddr == strCurrentAddr:
#
# Also update if it is more than a while since the last update.
# This stops the account terminating through lack of use.
#
if strLastUpdate - time.time() < (3600 * 24 * 14):
sys.exit()
#
# Create url to submit the request
# optional fields left blank
# This will handle all urls in one go.
#
strUrl = ("""http://members.dyndns.org/nic/update?
system=dyndns&
hostname=%s&
myip=%s""" % (",".join( strUrls), strAddr)).replace( '\n', '')
oReq = urllib2.Request( strUrl)
oReq.add_header("USER-AGENT", "Mozilla/4.76 en (X11; U; Linux 2.4.1-0.1.9 i586)")
oReq.add_header("AUTHORIZATION", string.strip("Basic " + base64.encodestring("%s:%s" % (strName, strPassword))))
#
# Dump out the responses. It should say 'good' for each.
#
# for strLine in urllib2.urlopen(oReq):
# print strLine
#
# Write updated address and time to config file.
#
open( "dynconfig.py", "wt").write( """strCurrentAddr = "%s"
strLastUpdate = %d
""" % (strAddr, time.time()))

