I sliced about 1/2" into the tip of my right index finger Saturday night with the ceramic utility knife I gave Tracy for her birthday (yes, the one I told her to be careful with because it'll cut just by looking at it). I was trying to be careful when I was cleaning it; that's why I cut my right hand. Grrr. I told the dude at the ER that I was cleaning it when it went off. Hardy har har.
Anyway, this post is gonna be short. Yeah, right..
If you have a machine on an Internet link, but which is NAT'ed, and your provider keeps messing with your IP address, then you might like this:
#!/usr/bin/perl -w
use strict; use LWP::Simple; use Mail::Sendmail; use Sys::Hostname; use Cwd;
# Define if you want messages on STDOUT my $debug = 0;
my $email_to = ''; my $email_from = ''; my $smtp_host = 'smtp.isp.com'; my $hostname = hostname; my $ipfile = cwd . "/ipaddress";
my ($oldip, $lasttime); my @oldip; my %mail;
my $content = get("http://www.whatismyipaddress.com/") || die $!; $content =~ /(\d+\.\d+\.\d+\.\d+)\s/; my $ip = $1; my $time = time;
if (-e "$ipfile") { open(IP, $ipfile) || die $!; chomp(($oldip, $lasttime) = <IP>); close(IP); } else { $oldip = "none"; $lasttime = $time; print "IP address file not found, will create.\n" if $debug; }
if ($ip ne $oldip) { open(IP, ">$ipfile") || die $!; print IP "$ip\n$time\n"; close(IP);
my $lastchangetime = scalar(localtime($lasttime)); my $currenttime = scalar(localtime($time)); my $message = "\nAs of $currenttime, $hostname reports your ip address as being $ip.\n"; $message .= "Your IP address last changed on $lastchangetime\n";
unshift @{$Mail::Sendmail::mailcfg{'smtp'}}, $smtp_host; %mail = ( To => $email_to, From => $email_from, Subject => "IP Address from $hostname", Message => $message ); sendmail(%mail) or die $Mail::Sendmail::error; print "\n\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log, "\n" if $debug; } else { print "IP address $ip has not changed, no mail being sent.\n" if $debug; }
That will email your IP address to you. I had a much simpler version up here originally, but it wasn't that useful.
This version will only mail you if your IP changes. So you can run this from a crontab like every 5 minutes and it will only bother you when you IP changes. It'll also let you know the last date and time a change was detected as well as the host that is reporting the IP.
Note the security risks with this. If a bad guy knows you use this to log (in at home or where ever), he can spoof it and then get into that box by fooling you into thinking your IP changed. Small risk, but there it is.
Oh yeah, that uses whatismyipaddress.com to screen scrape your IP. That doesn't have to be the case. If you have a web server somewhere on the outside from which you can run CGI scripts, then this will also work with the above script:
#!/usr/bin/perl -w
print "Content-type: text/plain\n\n", $ENV{REMOTE_ADDR};
Save that somewhere in a place where a browser can hit it and you're good to go.