HowTo: Automatically determine your public IP address and email it periodically

Let’s say you are running a poor man’s website where you are just testing stuff but have no real intention of buying a domain name or paying your ISP to give you a static IP address.

If you want to access your test site remotely, you need to know your public IP address, however your home ISP gives you a dynamic public IP address and every time you have a power failure, or reboot your router, you are assigned a brand new public IP address. This makes it very annoying if you are testing your site remotely.

Sure, you could use a Dynamic DNS service to keep track of when your public IP address changes, but what if you have a paranoid client who does not want to use even Dynamic DNS? How do you keep track of your new public IP without having to get to your internal network to read it each time?

What you need is a way to be able to have the system send you an email with your current public IP address so that there is no guess work involved. But how do we do this?

To determine your public IP, you need to have an external site to refer to which will record your public IP as connecting to it. You could use services such as What Is My IP or similar, but we don’t want to rely on an external service, nor do we want to hammer a third-party service with requests. Basically we just want to replicate the functionality of such a site as locally as possible and then use an email tool to send the information to you in a message.


  • Access to a web space outside of your working space with an IP or address that does not change. Generally every ISP out there will grant you some small quantity of complimentary web space to store some files. If you are working for clients like I am, you can use your own server if you like (after all, this blog is served from a fixed IP with a domain name attached to it).
  • A PHP interpreter in your web space’s host. If you are using your own static host on Ubuntu, Apache 2 from a normal Ubuntu install be it Desktop or Server, then you should have the PHP interpreter installed by default. If not, then install the php5 package.
  • If using your own static server, you will need a web server such as Apache installed on it to serve the PHP we’ll be creating.

Now let’s look at using a simple bit of PHP to obtain the address of the connecting host.

  1. On your external static web space (not your local dynamic site), create a new text file somewhere readily available called getmypublicip.php or similar.
  2. In your favourite text editor, copy and paste in the following PHP code:

    $ip = getenv("REMOTE_ADDR");
    echo $ip;

  3. Save your changes and exit the text editor.
  4. Test your new PHP file by referring to it from your ISP’s webspace, eg: If your ISP’s website was called and you had a login called “noddy”, then the address to your webspace might be, so type that in your web browser, which is usually the path most ISP’s use.
  5. When you go to that URL, the PHP on the remote side should parse the referring public IP address and echo it to the page. In this case, because you are referring yourself, you should see your own public IP address (let’s say it’s for the purposes of this guide) printed at the top-left of the web browser page.

    If you ran this PHP locally, you would get the internal IP of the connecting host instead, eg: or similar instead.
  6. Awesome, so now how do we email this newly obtained information to ourselves? Simple. We will make use of a neat little command line tool called sendemail (not to be confused with the venerable sendmail). It’s not installed in Ubuntu by default, so install it with:

    $ sudo apt-get install sendemail
  7. Let’s create the script that will use sendemail. On your local test website, create a new text file outside of your web-accessible directories, eg: /home/noddy/ using your favourite text editor.

    $ nano /home/noddy/
  8. In the text editor, copy and paste the following in:

    wget -O- 2>/dev/null | sendemail -u "Client's public IP address" -f -t -s

  9. Save your changes and exit your text editor.
  10. Now test your script my manually running it using the following command:

    $ sh /home/noddy/
  11. Check your personal email. You should have received an email sent by containing a single line showing your public IP address!
  12. Now we need to setup the script to be executed periodically. We will setup a cron schedule to run our new bash script as follows:

    $ crontab -e
  13. Your text editor will appear. In this, add the following line on the end:

    0 * * * * sh /home/noddy/
  14. Save your changes and exit your text editor.
  15. Now wait. On the next whole hour, you should receive an email in your mailbox containing a single line with an IP address – your public IP address! Now you need never know what your private server’s public IP address is if it changes in the middle of the day – on the hour you will be told what it is!
  16. If you’d like to receive notification on a more regular basis, modify the cron schedule. For example, the following change to step 13 will make your system run the script every 15 minutes:

    */15 * * * * sh /home/noddy/
  17. That’s it! Pat yourself on the back. You’re done.

NOTE: Some ISP’s do not allow connections on dynamic IP’s to send periodic email on their mail server and may block it on the belief that your emails may be spam. Should that be the case, you should install a local mail daemon such as Postfix and change the sendemail arguments to direct their mail to localhost instead, and allow Postfix to send the notification email. In Ubuntu, you can install Postfix by simply installing the postfix package and electing the server to be an “Internet Site”. The default configuration is fine, but not secure. Ensure that no-one externally can access port 25 on your client’s server if you choose to go down this route.