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]
Now you are ready to fill in the blanks. This is a Python3 script:
import ipgetter import urllib.request import requests currentIP = ipgetter.myip() #AutoRemote function to send a notification YOUR_KEY goes here def sendAR(x): AR_key = 'YOUR_KEY' AR_url = 'http://autoremotejoaomgcd.appspot.com/sendmessage?key='+ AR_key +'&message=IP%20' message = AR_url + x response = urllib.request.urlopen(message).read() print(x) #check the internet and check if previous file is present try: requests.get('http://www.google.com') print('Internet present') IPfile = open('ipfile.txt', 'r') lastIP = IPfile.read() if lastIP == currentIP: print('No changes last IP: ' + lastIP + ' current IP: ' + currentIP) else: with open('ipfile.txt', 'w+') as f: f.write(currentIP) f.close() sendAR(currentIP) print('IP updated') #handle no file except IOError: #print(IOError) with open('ipfile.txt', 'w+') as f: IPfile = ipgetter.myip() f.write(IPfile) f.close() print('created file with current IP') sendAR(currentIP) quit() #Handle no internet except requests.ConnectionError: quit()
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.
Create new line ie:
0 7 0 0 0 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.