Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A Perl script to check for IP address changes Network
I use one of my Macs to run a webserver, but my ISP does not support static IP addresses. So my IP address changes sometimes, causing some of my websites to become unavailable until I can update the IP address. With services like dyndns.org, you can do this automatically, but not all services support automatic updates. So I wrote a Perl script to have me notified by email whenever my IP changes. This way, I can minimize the time my sites are offline.

In addition to the script, you need a file that contains your current IP address and you need to add a cron job to run the script as often as you like (it also uses sendmail to send the message). I use checkip.dyndns.org to check the IP address. There might be an easyer way to obtain the WAN address, and suggestions are welcome!

Here is the script:
#!/usr/bin/perl -w

use strict;

my $mailprog = '/usr/sbin/sendmail';
my $ip_last_check_file = "/Path/To/Current/IP/Address";
my $ip_now = `curl -s http://checkip.dyndns.org`;
$ip_now =~ s/.*?(\d+\.\d+\.\d+\.\d+).*/$1/s;

open(IP, "<$ip_last_check_file") or die "Cannot open $ip_last_check_file: $!";
my $ip_last_check = <IP>;
close(IP);

if ($ip_now ne $ip_last_check)
{
  open(IP, ">$ip_last_check_file") or die "Cannot open $ip_last_check: $!";
  print IP $ip_now;
  close(IP);

  open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";
  print MAIL "From: user\@domain.com\n";
  print MAIL "To: user\@domain.com\n";
  print MAIL "Subject: Your IP-address has changed\n";
  print MAIL "Your IP-address has changed to $ip_now\n";
  close MAIL;
}

exit;
Make sure you remember to make the script executable (chmod +x script_name).

[robg adds: A couple of previous hints explain how to do similar things via AppleScript and by using an intermediate server to redirect to the current IP address. As noted, dyndns.org can also do this automatically...]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[19,111 views]  

A Perl script to check for IP address changes | 11 comments | Create New Account
Click here to return to the 'A Perl script to check for IP address changes' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Flakey static IP
Authored by: derekhed on Oct 27, '04 12:41:18PM

I too am tracking my IP, since sometimes my ISP cannot be trusted and I'll need to access my home server from work. Luckily my ISP (like most) gives me web-space on their server, so I use this bash script to stick my current IP there. You'll need a .netrc with your ftp settings for this to work, and of course the requisite crontab. I had to work out the ftp commands by brute testing, see what works for you.

#!/bin/bash
page=/Users/your_home/ipaddress.html
curl -s http://checkip.dyndns.org > $page
ftp <<**
open home.ISP.net
epsv4
lcd
ascii
put $page ipaddress.html
bye
**



[ Reply to This | # ]
Flakey static IP
Authored by: lullabud on Oct 27, '04 12:52:27PM
A much simpler way to do this is using curl for the upload and skip the temp file and .netrc:

curl -s http://checkip.dyndns.org | curl -T - ftp://username:password@your-domain.com/www/ip.txt


[ Reply to This | # ]
Flakey static IP
Authored by: derekhed on Oct 27, '04 03:19:00PM

Niiiice. Thanks!



[ Reply to This | # ]
Very Handy
Authored by: lullabud on Oct 27, '04 12:46:52PM

There is no doubt, knowing when your IP# has changed is very handy, for both dynamic IP#'s and for people like me who switch their laptop between several dhcp LAN's.

Fink offers the ddclient package, which has e-mail support built in, as well as a choice between local IP# and WAN IP# updating. Of course, Fink requires the dev tools, so it's not an option to everybody, which is precisely where a script like the one listed above would come in extra handy.

Another website providing IP# discovery is simply whatismyip.com



[ Reply to This | # ]
A Perl script to check for IP address changes
Authored by: mzs on Oct 27, '04 01:22:03PM
You most likely get your dynamic IP from DHCP. You might look at this hint to see how to use Kicker.app to only do this when needed instead of with cron. Then it would be instantaneous. In fact there are ways to use DyDNS to have names point to valid addresses without sending emails.

[ Reply to This | # ]
A Perl script to check for IP address changes
Authored by: balston on Oct 27, '04 02:03:03PM

As a sidenote ... make sure you check with your ISP to make sure that they do not block port 80 through their system.

I am using Adelphia (BLAH) cable, and not only do they block port 80 they also explicitly state that if you run a server of ANY kind that they will terminate your service.

Sure ... DynDNS has a way around the port problem but if your ISP decides to monitor the increased activity to your IP you may be in trouble.

Sadly ... I had to take my site down because I don't want to lose my internet capability as Adelphia is the ONLY high speed provide in my area :-(



[ Reply to This | # ]
A Perl script to check for IP address changes
Authored by: balston on Oct 27, '04 02:04:37PM

As a sidenote ... make sure you check with your ISP to make sure that they do not block port 80 through their system.

I am using Adelphia (BLAH) cable, and not only do they block port 80 they also explicitly state that if you run a server of ANY kind that they will terminate your service.

Sure ... DynDNS has a way around the port problem but if your ISP decides to monitor the increased activity to your IP you may be in trouble.

Sadly ... I had to take my site down as I don't want to lose my internet capability as Adelphia is the ONLY high speed provide in my area :-(



[ Reply to This | # ]
A Perl script to check for IP address changes
Authored by: tmb on Oct 27, '04 07:00:58PM
apart from emailing the result you might as well upload it to your webserver. i'm using the Net::FTP package for that. thus i'm able to figure out my ip even when not on my own mac.

my $ftp = Net::FTP->new("$FTP_SERVER", Debug => $DEBUG, Passive => 1);
$ftp->login("$FTP_USER","$FTP_PWD");
$ftp->cwd("router_ips");
$ftp->put("$PATH/$FILE");
$ftp->quit;
cheers,
-tmb

[ Reply to This | # ]
consider cfengine instead of random scripts
Authored by: thrig on Oct 27, '04 09:51:45PM

Instead of adding one-off scripts to do random things to your system, consider using cfengine, which becomes the framework for how things are managed on your system. This has a higher initial learning curve than writing random scripts, but should pay off if you have multiple systems you all want to behave from a central configuration source.

More tips on cfengine, with Mac OS X notes.



[ Reply to This | # ]
Keeping a Historical List of IPs
Authored by: MartySells on Oct 29, '04 03:08:09AM
One thing I added to my implementation of this type of script is a feature that keeps a historical log of when each IP was in use. This is handy if you get asked what your IP was on a particular date or if you want to have an idea of how long your dynamic IPs last.

First, a script that I call iplog (you will have to fix the absolute path but be sure to keep the double >>):

  #!/bin/sh
  DATE=`date +%F,%T`
  EPOCH=`date +%s`
  IP=`curl -s 'checkip.dyndns.org' | tr -C -d '0-9.'`
  echo $IP,$EPOCH,$DATE >> /Users/msells/ip-log.txt
Then something like
  @hourly /Users/msells/bin/iplog
in your crontab.

This can run along side whatever other (email me, create a web page, etc..) solution you use to notify you of changes to your IP address - it's just keeping a local log of what IP was in use. The log file itself is three comma separated fields: IP, seconds since epoch and a human readable datestamp. Accordingly, here are some handy things:

  alias myip='tail -1 /Users/msells/ip-log.txt | cut -d, -f1'
  alias iphistory='cut -d, -f1 /Users/msells/ip-log.txt | uniq -c | tail -5'
The first column in the iphistory is the number of periods (hours in this example) that the given IP was in use with the last one listed being the current IP.

If you want to setup your own CGI that tells you your current IP here's how to in various scripting languages:

  #!/bin/sh
  echo 'Content-type: text/html'
  echo
  echo $REMOTE_ADDR

  #!/usr/bin/perl
  print "Content-type: text/html\n\n" . $ENV{'REMOTE_ADDR'} . "\n";

These, of course, go on an Internet exposed web server with a fixed address, not your local system.

-m

[ Reply to This | # ]
A Perl script to check for IP address changes
Authored by: marc k on Feb 08, '05 12:42:25AM

or you could just use www.no-ip.org
it's free



[ Reply to This | # ]