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.
import urllib2
import string
import base64
import re
import sys
import time
try:
from dynconfig import *
except:
strCurrentAddr = None
strLastUpdate = 0
strRouterName = <CENSORED>
strRouterPassword = <CENSORED>
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))))
bWan = False
for strLine in urllib2.urlopen(oReq):
if bWan == False:
if strLine.find( 'WAN') >= 0:
bWan = True
else:
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"
if 1:
strUrls = <CENSORED>
strName = <CENSORED>
strPassword = <CENSORED>
else:
strUrls = ( "test.homeip.net", "test.mine.nu")
strName = "test"
strPassword = "test"
strAddr = "1.2.3.4"
if strAddr == strCurrentAddr:
if strLastUpdate - time.time() < (3600 * 24 * 14):
sys.exit()
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))))
open( "dynconfig.py", "wt").write( """strCurrentAddr = "%s"
strLastUpdate = %d
""" % (strAddr, time.time()))
|