External IP Address Monitor Script Andrew Mallett | Updated July 2017

This simple script will monitor the external IP Address of a system connected to the internet. It can be used for a stand-alone workstation or as in this case, from inside a LAN. In this example all workstations and servers connect to the web via an ADSL Router (I actually use a Linux Mint box connected to an ADSL router, but most users will have an off-the-shelf broadband router box). The router has two network interfaces with two separate IP Addresses..

LAN and Gateway..

The internal-facing interface on the router has a class-C IP Address of All systems wanting to connect beyond the LAN talk to this interface (their Default Gateway). The second network interface on the router (shown as 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 periodically and also 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. Through the wonders of PHP, this file returns the IP Address of the outward-facing gateway/router interface..

Request for external IP..

1. The Shell Script

The script grabs the ip.php file from the web server. If you have access to a web server running PHP then the PHP code is listed in section 2 below. Otherwise point wget to a known web server which is running the code, such as the one here, at shell-scripts.com..

cd /scripts
wget http://shell-scripts.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 you want to run your own PHP script, then create a clear text file called ip.php and add the four lines below..

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

Very useful for embedding into a web page using a PHP include..

<?PHP include('http://shell-scripts.com/ip.php'); ?>

(Note that due to security risks, PHP includes from other domains are often disabled)