Next let's talk about the local machine side. On one of the PC's on your home LAN you need to place the script named iplog.pl. This script polls ipreflect.cgi to discover your router's current IP address. The script then records this IP address to a local logfile. And if the IP address is different from the one previously recorded, the script sends an email to your webhost account containing your new IP address.
Now for the final details.... You'll need to set up a .forward file and a .procmailrc file on the web host so that when this New IP email arrives at the web host, the email is logged into a file that currentip.cgi can read.
Now I'll post the code.....
.forward
|exec /usr/bin/procmail
:0 * ^Subject:.*\[IP Log\] /home/your_homedir/path_to_file/ip.txt
ipreflect.cgi
#!/usr/bin/perl print "Content-Type: text/html\n\n"; print defined $ENV{REMOTE_ADDR} ? $ENV{REMOTE_ADDR} : "Not Available" , "\n";
#!/usr/bin/perl use strict; use warnings; my $logfile = '/home/your_account/path_to_file/ip.txt'; open my $inlog, "<", $logfile or die "Couldn't open $logfile.\n$!"; my $ip = ''; while ( my $line = <$inlog> ) { next unless $line =~ m/^IP:\s*\d+\.\d+\.\d+\.\d+\s*$/; chomp $line; $ip = $line; } close $inlog; print "Content-Type: text/html\n\n"; print << "HTML"; <body> <h2>$ip</h2> </body> HTML
iplog.pl
#!/perl/bin/perl.exe use strict; use warnings; use LWP::Simple; use MIME::Lite; my $reflector = "http://your.webhost.com/cgi-bin/ipreflect.cgi"; my $logfile = "ip.log"; # Get IP from an IP reflector server. my $ip = get $reflector; chomp $ip; my $now = localtime; $ip = "Reflector server down." unless ( $ip =~ m/^\d+\.\d+\.\d+\.\d+$/ or $ip =~ m/Not Available/i ); # Read log to determine if the IP changed since last test. my $ip_changed = ""; if ( -e $logfile ) { open my $inlog, "<", $logfile or die "Can't read logfile.\n$!"; while ( <$inlog> ) { next unless eof; chomp; my $last_ip = ( split /\s+=>\s+/ )[1]; $last_ip =~ s/\*$//; $ip_changed = ( $last_ip ne $ip ) ? "*" : ""; } close $inlog; } else { # If the logfile doesn't exist, assume IP changed. # This forces an update to the remote server anytime # the logfile is deleted. Remember, for a fresh start # at both ends, just delete the local logfile. $ip_changed = "*"; } # Update log. open my $log, ">>", $logfile or die "Can't open output logfile.\n$!"; print "$now => $ip$ip_changed\n"; # Screen output. print $log "$now => $ip$ip_changed\n"; # Logfile output. close $log or die "Couldn't close output file.\n$!"; # If the IP changed, send the new IP to remote server via email. if ( $ip_changed and $ip =~ m/^\d+\.\d+\.\d+\.\d+$/ ) { my $msg = MIME::Lite->new( From =>'your@email.address', To =>'your.email@address.on.web.host', Subject =>"[IP Log] $now", Data =>"Updated: $now\nIP: $ip\n" ); MIME::Lite->send( 'smtp', "your.isp.smtp.server", Timeout => 60 ); $msg->send; }
I set iplog.pl up in a Windows Task Scheduler job on my local PC to run once per hour. Every hour it will poll the remote webhost to see what IP address the webhost thinks my router is coming from. The results will be written to the logfile. And if the IP has changed, notification will be emailed to my webhost account email address. procmail will catch that message and save it to a logfile. The next time I access currentip.cgi at my webhost, I'll see my current router IP, even if I'm checking remotely.
For this to be effective, the router must be configured with Port Forwarding so that it will know which service requests to pass on to which PC's on my local network. Frankly, I don't use it for much of anything other than testing CGI scripts on my local machine from a remote machine when I'm away from the local machine. It's mostly just a testing tool for me, and something to tinker with.
But I thought the method used was just crazy enough to be worth posting. Comments welcome.
The biggest shortcoming this has is that the local logfile, and the remote one, will grow and grow forever. Perhaps if I was serious about using this I would build tools to keep them reasonably sized.
The other shortcoming is the fact that it requires that the user actually have some external web host that allows cgi scripting.
ipreflect.cgi could just as easily have been written as a html document with server-side-includes, but I was on a CGI kick and just wrote it as a CGI script.
Why did I use ipreflect.cgi on a remote host, rather than allowing iplog.pl to just poll my router through its built-in web interface? That would be because I have a Netgear WGR614v4 router. Its web interface requires javascript to work properly, and I have no idea where to begin on programming a javascript enabled web-scraper script. Thus, this convoluted mess you see posted here. ;)
Enjoy.
Dave
In reply to Broadband Dynamic IP Poster by davido
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |