This describes a little utility that I wrote in order to find out the dynamic IP address of my Linux box which is connected via ADSL broadband to the internet. This is actually a development of a much more convoluted system originally written in the days when diallup was the only option and I needed to be able to telnet or SSH back into the box maintenance purposes.
It uses PHP on the server side, and the Linux box runs a companion Perl script.
Time for a short disclaimer..
.
The system I have built works fine for me - as always, your mileage may vary. There is no warranty with this product.
Fairly minimal:
Ever since I started using the internet seriously, for most of the time have been held back by having a dynamic IP address. OK, call me a cheapskate but I wanted to be able to connect to my box from anywhere when it was online and this ideally meant having a static IP address. Until I changed to Demon as an ISP this wasn't going to happen (too expensive), and for a while things worked very well. Then came broadband. Changed ISPs again, and found myself with a cool router and.. another dynamic IP address.
However, I was able to confirm that I was in fact allowed under the new T & Cs that I was permitted to connect to my broadband-connected box, and in fact I'm writing this in an SSH session right now. But still that dratted address change. In some respects it was worse under broadband as I never knew what conditions caused the IP address to change, so I sought advice in the newsgroups. "Try a dynamic DNS service" said several people...
Well call me paranoid, but when dynamic DNS services came out, I had a look, and decided that I didn't really want to rely on a third party to manage access to my system. Also although I don't hold State Secrets on my Linux box I felt that the fewer people that knew about it the better, especially bearing in mind the number of intrusion attempts I get on a regular basis.
So, the "WHEREAMI" system was developed and deployed.
There are two sides to this - client and server side - and I'll cover the server side first as it's really simple.
Click
here
to launch this in a new window.
This page is simple in the extreme. When it is called, it simply prints the PHP variable $_SERVER['REMOTE_ADDR'] . Not only that, it returns the address as a comment within the web page, so the calling software (normally a web browser) will not display anything on the browser screen. However you should remember that we're not going this with a web browser, but a Perl script :-) And remember also, it will display the IP address of the system that called the page, which is of course your Linux box.
Looking at the source, here's typically what you'd get:
<!--*191.242.6.10*--> Hey ! That would be your address !
So, armed with the knowledge that every time the page is called, you'll get back your own addresss, and this leads us nicely into Part 2.
It's a little more complex on the server side. This Perl script called "whereami.pl" will be run from the box whose IP address you need to track.
01 #!/usr/bin/perl -w
02 use Net::SMTP;
03 use LWP;
04 my $URL = 'http://www.yourserver.com/whereami.php';
05 my $req;
06 my $ua;
07 my $res;
08 my $content;
09 my $debugging;
10 my $smtp;
11 my $message;
12 my $subject;
13 $ua = new LWP::UserAgent;
14 $ua->agent("$0");
15 $req = new HTTP::Request('GET', $URL);
16 $res = $ua->request($req);
17 die "\n" unless ($res->is_success);
18 $content = $res->content;
19 # Looking for this pattern: <!--*210.111.151.47*-->
20 if ( $content =~ m/\.*(\d{1,}[.]\d{1,}[.]\d{1,}[.]\d{1,}).*/o )
21 {
22 $message = $1;
23 $subject = $1;
24 }
25 else
26 {
27 $message = "Wierd .. ISP said \"$content\"";
28 $subject = "Unexpected response";
29 }
30 #
31 open (DAT, "</tmp/whereami.dat");
32 my $oldlocation = ();
33 close DAT;
34 chomp($oldlocation);
35 #
36 if ( $oldlocation ne $subject )
37 {
38 print "New IP address $subject - was $oldlocation\n\n";
39 open (DAT, ">/tmp/whereami.dat");
40 print DAT "$subject\n";
41 close DAT;
42 #
43 my $Domain = 'yourdomain.com';
44 my $MailDest = 'yourname@youremailaddress';
45 my $Sender = 'yourname@youremailaddress';
46 my $ReplyTo = $Sender;
47 $smtp = Net::SMTP->new( $Domain, Hello => $Domain, Timeout => 30, Debug =>$debugging );
48 $smtp->mail( $ReplyTo );
49 $smtp->recipient($MailDest, { SkipBad => 1 });
50 $smtp->data();
51 $smtp->datasend("Subject: $subject\n");
52 $smtp->datasend("Importance: high\n");
53 $smtp->datasend("X-Mailer: $0\n");
54 $smtp->datasend("IP address has changed to $message");
55 $smtp->datasend("\n");
56 $smtp->dataend();
57 $smtp->quit;
58 }
59 exit;
Not too bad.. let's have a walk through.
Lines 01-12 Tell the OS that it's a Perl script, and in addition, our script variables are defined.
Lines 13-14 Start up a new 'web browser' - really a user agent
Lines 15-16 Fetch our PHP web page from the web server
Lines 17-17 Die nicely if someone has removed it..
Lines 18-18 We store the downloaded web page content into a variable called '$content'
Lines 20-20 This is a regex that looks for a dotted quad IP address
Lines 21-24 Sets variables for a successful match
Lines 23-23 NB: Note that if successful, the IP address is now stored in the subject line !
Lines 26-29 Sets variables for an unsuccesful match - possibly a 404 error
Lines 31-33 This reads the IP address from the previous run 1
Lines 36-36 Comparison between previous IP address and one we just extracted
Lines 38-41 Now we can store our new IP address in the file
Lines 43-57 This sends an email with the new IP address
Lines 44-45 This is the email address that you wish to receive notification at: typically a Hotmail, Yahoo or other webmail-based account.
If you are able to send SMS text messages to your mobile phone via email, you can also keep informed this way simply by adding the mobile phone account as an extra recipient !
Of course all these neat stuff is useless if you have to run it by hand, so our old friend CRON comes into action. I have set up a crontab entry on my box that runs the Perl script every 1 minute past the hour between 8am and 6pm, Monday - Friday. All the rest of the time I would normally be at home and would know the IP address had changed by the howls of anger from the various users around the house...
01 08-18 * * 1-5 /usr/local/sbin/whereami.pl
If you're off on an extended trip, or want it going all the time, try this one:
01 * * * * /usr/local/sbin/whereami.pl
So, in actual use, when you're away and you check your Hotmail account, you may get a message from 'yourself' to say that your Linux box' IP address has changed to (say) 191.242.6.10, and to access it, you can simply type:
| http://191.242.6.10/ | for your web server |
| ftp 191.242.6.10 | for your ftp server |
| ssh 191.242.6.10 | for SSH access |
| telnet 191.242.6.10 | for telnet access |
| ... and so on |
Download the Perl script and PHP web page (you will need to get the Perl modules direct from cpan.org - see below):
The ZIP archive contains the following files:
Archive: whereami.zip
Length Method Size Ratio Date Time CRC-32 Name
-------- ------ ------- ----- ---- ---- ------ ----
18011 Defl:X 6829 62% 12-12-01 12:01 0f4438ca gpl.txt
252 Defl:X 144 43% 08-13-03 12:54 82629fdc whereami.php
1462 Defl:X 694 53% 08-13-03 12:52 7958a627 whereami.pl
-------- ------- --- -------
19725 7667 61% 3 files
libnet Perl module
libwww Perl module
cpan.org A great Perl repository
php.net A Great Scripting Tool