A few weeks ago, I posted about how to send SMS messages via command line. Today, as promised, I am going to follow up on that post by providing a practical use for this functionality.
My current setup at home includes a hole in my firewall to allow SSH access to a machine sitting inside of the network. On this machine sit various items that may be useful to me when traveling outside of my home network, including movies, music, and various other files. However, this open SSH hole does me zero good if I don’t know what my machine’s IP is, and I generally would not know since the IP is dynamically allocated by my ISP.
In order to always know where my machine is currently located, it needs to phone home every time the allocated IP changes. In this case, I mean to phone home literally. I have setup this particular machine to regularly check its IP, then, if the IP has changed since it last checked, it will send me a text message with the new IP.
EDIT: The Perl module used in my SMS perl script has since stopped working. For this reason, I suggest using the modified version of my code presented by David, which relies on SMS provider email gateways to achieve the same end result.
This setup is actually very simple. A number of websites, including ipecho.net/plain and curlmyip.com, provide the plaintext of the requester’s IP. With this current IP, plus a cache of what the IP used to be, we can detect whenever the machine’s IP has changed and send an update indicating this new IP.
The code above first sends an HTTP request to retrieve the current public IP. If the request fails then the script dies since there is nothing more that can be done. If the request does not fail, then the value returned is compared against the cached IP. If the compared values are different, then we update the cache and send an SMS message indicating the change.
With this code written, all that is left to do is have the script run at regular intervals using cron. I have mine running every 10 minutes, but you should base yours on how often you notice your ISP changing your public IP plus how accurate you want your IP record to be.
And that wraps up this post. If you found this interesting or if you have any suggested improvements, don’t hesitate to post a reply!