This simple script will monitor the external IP Address of a network connected to the internet, from a 'nix box inside the LAN. The network setup is typical of a small to medium sized LAN, with various systems running internal IP Addresses, all of which point to the internet-connecting device (i.e. ADSL Router) as the Default Gateway. The router has two network interfaces with two separate IP Addresses..
The internal-facing interface on the router has an IP Address from the same range as the users on the LAN, in the example above 192.168.0.1. Thus all systems wanting to connect beyond the LAN talk to this interface (their default gateway).
The second network interface on the router points outwards to the internet and is allocated a dynamic or static IP Address from the Internet Service Provider. If the external IP is static then it is known. However many service providers allocate an IP from a pool of addresses, which may change if the router is reset.
The shell script uses the wget command to obtain a file called ip.php from a web server out on the internet. This clear text file contains the IP Address of the outward-facing router interface..
1. The Shell Script
The script in its simplest form simply grabs the ip.php file (or whatever it's called) from a web server which is running the service. If the user has access to a web server running PHP then the PHP code is listed in section 2. Otherwise point wget to a known web server which is running the code, such as the one at biranchi.com..
cd /scripts
wget http://biranchi.com/ip.php
Note that wget will download the file to the local directory, which is the current directory on the 'nix system when the script is run. To ensure this location is known, I have created a directory called scripts which is changed to in the first line of the script above.
Perusal of the downloaded ip.php file reveals the external IP of the router/gateway.
2. The PHP Script
If the user wishes to run their own PHP script on a web server, then create a clear text file called ip.php and add the four lines below..
<?
$ip=$_SERVER['REMOTE_ADDR'];
echo $ip;
?>
Upload the file to the root web directory of a webserver (which is running PHP) and simply use that hostname instead, i.e. wget http://yourwebser.com/ip.php
Tweaking things - logging it
As with all things Unix, this script can be tweaked to buggery. I have created a script extip.sh which checks my external WAN IP on a daily basis and records it to a log file..
cd /sc/extip
rm /sc/extip/ip.php
wget http://biranchi.com/ip.php
echo `/bin/date +%Y-%m-%d` `/bin/cat /sc/extip/ip.php` >> /sc/extip/ip_record
In the above script I have previously created a container directory called extip. On the second line the previous day's result, ip.php is firstly deleted. The script then wgets a new ip.php..
Finally the echo command is used to echo the current date and then the contents of the current ip.php file (the current IP Address) and append the result to a log record file called ip_record.
The task is automated at two minutes past midnight via a new crontab entry..
02 00 * * * /sc/scripts/extip.sh
Remember to use full paths to all files, within scripts which will run via cron.
Tweaking things - reporting it
This next modification checks the external IP at two minutes past midnight, logs it as before, then compares it to the previous day's IP. If the external IP Address has changed, the script sends an administrative alert via email.
cd /sc/extip
rm /sc/extip/ipold.php
mv /sc/extip/ip.php /sc/extip/ipold.php
wget http://biranchi.com/ip.php
echo `/bin/date +%Y-%m-%d` `/bin/cat /sc/extip/ip.php` >> /sc/extip/ip_record
if [ $(cat /sc/extip/ip.php) = $(cat /sc/extip/ipold.php) ]; then
echo
else
/sc/scripts/extip_email.sh
fi
The above script firstly changes to the working directory /sc/extip and deletes ipold.php, which contained the IP record for the day before yesterday (remember this is running at 00:02 hours on a new day).
Line 3 then moves yesterday's IP record ip.php to ipold.php
Line 4 grabs the new IP address from the web server, puts it into a new ip.php file and updates the ip_record in line 5..
The if-else statement compares today's ip.php with yesterday's ipold.php to see if they are the same. If the are, it echoes nothing and finishes. If they are different then the statement else's out to send an email (or even an SMS) via the separate email script - extip_email.sh
.
- A.