Tuesday, November 21, 2006

I Know my Public IP Address. Can You Find Yours?

I finished a cool little script yesterday and I am quite proud of myself, even though there's one part I just completely took for granted, hacked at it and made it work. But it does.

At home I have what's called a dynamic IP address, as most residential customers do. This means that one's public IP address can change depending on the ISP's needs and the customer's usage. If you leave your modem off for a week, you are almost guaranteed to be issued a different IP address than the one you had before.

I often like to get back to my systems at home for whatever reason (eventually I'll be able to turn the lights on and off remotely - a few work now). To do this I need the IP address that my ISP has given me. Knowing that it could change, I needed some way to know what it had changed to. Therein begins the logic: I know that one can go to www.whatismyip.com and see their public IP address. I also know that I can write a program in Perl to surf the web and look for stuff. I put the two together and employed my 10-year-old Compaq to run the program every day.

It actually sends it to my Gmail address, which is then filtered, tagged, and archived automatically. I don't have to delete the message every day - I can find the latest message in an instant, and access is mine.

The finished file looks like:

#!/usr/bin/perl

#Title: IP Sender
#Author: Ben Rehberg
#Version History:
#02/14/2006 Creation
#
#11/20/2006 Finally got that regular expression right
# to extract the IP from the web page. Done.
#
#########PROGRAM DESCRIPTION###########################
#
#I want this script to check whatismyip.com every
#morning and e-mail me with the address.



#variables and settings################################
($sec,$min,$hour,$mday,$mon,
$year,$wday,$yday,$isdst)=localtime(time);
$mon+=1;
$year+=1900;
my $date="$mon"."/"."$mday"."/"."$year";
my $url = 'http://www.whatismyip.com';
my @recipients = ('spaminator@benrehberg.com',
'daily_crap@techpickle.org');

#begin program code#####################################

use LWP::Simple;
my $content = get $url;
die "Couldn't get $url" unless defined $content;

$ip = "I can't find it.";

if ($content =~ m/(([0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})/){
$ip = $1;
}
#print $ip;
#print $content;
#print $date;

require Mail::Send;
$msg = new Mail::Send Subject=>"Your IP Address for ".$date;
$msg->to(@recipients);
$fh = $msg->open;
print $fh "Your IP Address this morning is ".$ip;
$fh->close;
So there it is. Feel free to copy this and use it for yourself. I provide no warranty and assume you know what you're doing if you choose to take it. Be advised that you will have to install the Mail::Send and LWP::Simple modules in order for this to work. The LWP library was already installed on my system, but I had to get Mail::Send from CPAN. Just follow the link and the instructions.

Questions? Feel free to comment with them or e-mail me.

Update: I forgot to tell you that you must drop this file in /etc/cron.daily so the system will run it on a schedule. Also make sure you make the file executable (chmod 755).

No comments: