Rapsberry Pi – Dynamic IP updater

One of the readers asked me how I found the IP address of my Raspberry Pi from China. I had DNS and my IP is fairly static, but that got me thinking. You could install automatic updater and use free DNS like noip.com. What if you want to know your actual external IP when it changes? The Dynamic IP updater – is here to help you.

Dynamic IP updater

This is a Python3 script that runs on a Raspberry Pi, monitors the changes every X minutes and notifies you if the address changes, by sending your an AutoRemote message. If you don’t know what AutoRemote is here is more about how to use it with Raspberry Pi. The message can be displayed on a PC, or mobile and used with Tasker.

While IP can be scraped from a website, I found a neat library on github. Make sure you have pip installed:

sudo apt-get install python3 pip

and then install the library from github:

sudo pip3 install ipgetter

You will need to get the google key from AutoRemote for each device you want to notify about the IP change. Just visit associated AutoRemote URL and you will be able to get it:

http://autoremotejoaomgcd.appspot.com/?key=[it's the part that is here]

Short explanation. Initially, the script will create the file and issue an AR notification. If you want to notify more than one device, duplicate the sendAR() function [sendAR1(), sendAR2()], changing the keys and add the newly created functions to the script.

When the script runs, the new IP is compared to the one in the file, and if a difference is found, a notification is issued. Otherwise, nothing happens as there is nothing to report. There is also an exception to terminate the script if no network is present.

Scheduling the dynamic IP updater

Instead of running this script constantly in the background schedule it to run as often as you wish with a crontab.

crontab -e

Create new line ie:

0 7 * * * python3 /path/script.py

if you want to run the script once a day at 7:00 am. You can read more on scheduling with crontab here.

Support me! – It takes several hours to get a single tutorial ready (idea, research, prototype, testing, writing a guide, making a video). All that in my spare time. Consider supporting me as it helps me grow, start new projects and hopefully turn this hobby into a full-time profession. Donate any amount you wish via PAYPAL or pick one of the options below: