The LAN in my house is connected to the internet via broadband. Like most home users, I don't want (or need) to pay for a static IP address. I was looking for a roll-your-own alternative to dyndns.com, and I found this perl script... somewhere. This script allows me to keep a server on my LAN and make it accessible from the internet by opening up the appropriate ports on my router. I retained the original author's comments so you can track him down if you want. It was written for Windows, so I had to do some modification to get it to work under Darwin (AKA Mac OS X/FreeBSD).
The following script was a small piece to the puzzle of setting up an open-source digital music system in my house. After I discovered NetJuke, I quickly realized that if I setup HTTP port forwarding on my router, I could access my music from anywhere on the internet while I was away from home. This would allow me, for example, to update my 5GB iPod while on the road. But if my IP address changed, I'd have to figure out what the new IP address was before I could make it work. (I'm not even sure how to do it.) If I could get my computer to do it for me, it would be one less thing I'd have to think about.
The script polls a public server to determine my LAN's IP address, then, if it's changed, posts the IP address to a URL which contains a redirect on a static IP server. I setup a cron job to run it every 15 minutes so that if my LAN's IP address changes, it's only invisible for a short time.
#!/usr/bin/perl
# GetWANIP.PL
# by Jason E Moyer (JasonEMoyer@hotmail.com)
# Used to Post WAN IP address
#
# Adapted for Mac OS X by Charles Estabrooks
# (Charles at Estabrooks dot org)
# Removed Win32 module and added LWP
# Modified $strEndWAN to search for "</body>" instead of "<br>" tag
# Changed the content of file to be ftp'd to contain a redirect tag
# instead of just a link
# Added two string variables for FTP server name and Localhost URL
# for redirection
# 04/26/04 Added check to compare old IP to current IP before
# executing FTP.
use LWP::Simple;
use Net::FTP;
#####################
# Set Default Values
# Replace all of "your" values with your actual values
$strCheckWAN = "http://checkip.dyndns.org/"; # Check WAN IP Address
$strStartWAN = "Current IP Address: "; # Identify Start of WAN Address
$strEndWAN = "</body>"; # Identify End of WAN Address
$strDate = localtime(time); # Sets current time for timestamp
$strFTPUser = "your_ftp_user_name"; # Username to use with FTP Site
$strFTPPass = "your_password"; # Password to use with FTP Site
$strFTPServer = "your.ftp.server"; # FTP Server Name
$strFTPDirectory = "/home/your/ftp/dir";# FTP Directory to put file
$strFTPFileName = "index.html"; # Name of the file to post
$strOLDIPFile = "/var/tmp/oldip"; # File containing old IP address
$strLocalhostURL = "/index.html" # URL on local host to point to
$strOLDIP = "";
#####################
# Grab Current WAN IP Address
$strFILE=get($strCheckWAN);
#####################
# Parse String to Extract WAN IP Address
$intSTART = index($strFILE, $strStartWAN) + length($strStartWAN);
$intEND = index($strFILE, $strEndWAN);
$intWANSize = $intEND - $intSTART;
$strWANIP = substr($strFILE, $intSTART, $intWANSize);
print ("Current WAN IP address: ", $strWANIP, "\n");
# Read old IP address from saved file
if ( -e $strOLDIPFile) {
open (fileOLDIP, "<".$strOLDIPFile);
$strOLDIP = <fileOLDIP>;
close (fileOLDIP);
print ("Old IP address: ", $strOLDIP);
}
# Compare to old WAN IP Address
# If different, Update
#
if ($strWANIP != $strOLDIP) {
open (fileOLDIP, ">".$strOLDIPFile);
print fileOLDIP "$strWANIP\n";
close (fileOLDIP);
#
# Create the web page with Hyperlink and time stamp.
print ("Writing new IP address to file: ",$strFTPFileName, "\n");
open (fileOUT, ">".$strFTPFileName);
print fileOUT '<html><head><META http-equiv="Refresh" \
content="5; URL=http://'.$strWANIP.$strLocalhostURL.'"> \
</head><body><br><br>Loading - Please wait a \
moment...</body></html>';
close (fileOUT);
##
## FTP the web page to the web site
##
print ("Uploading to ftp server: ",$strFTPServer, "\n");
$ftp = Net::FTP->new($strFTPServer, Debug => 0);
$ftp->login($strFTPUser,$strFTPPass);
$ftp->cwd($strFTPDirectory);
$ftp->put($strFTPFileName);
$ftp->quit;
}
else {
print ("IP address is unchanged.\n");
}