Home Raspberry Pi Rapsberry Pi - Dynamic IP updater

Rapsberry Pi – Dynamic IP updater

Your IP changes often? Always stay up to date without a DNS!

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:

https://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 = 'https://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('https://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.

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 schedule with crontab here.

Project Download

Download project files here. Bear in mind that Patreon supporters have early access to project files and videos.

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated in style with crypto-currency

SATA HAT

Add an SSD to Raspberry Pi 4 and turn it into a super-cooled NAS drive.

More HATs

Argon One

Enclose Raspberry Pi 4 inside this great case with custom I/O, cooling and GPIO

More cases on

Best Raspberry Pi Projects

How to use Raspberry PI as WOL (wake on lan) server

0
While you could wake up your PC from a mobile directly, having a dedicated server capable of doing so is the best solution. The reason is simple. You can hook up as many devices as you wish with a single endpoint. This is why Raspberry Pi is perfect for this.

Free wireless doorbell (Amazon Dash button hack)

0
The most terrifying situation I can imagine* is missing a parcel

Raspberry Pi NAS: (NAS-pi) Modding like a boss #part2

0
Plex and RAID1 NAS drive build log

MagPi – auto-downloader

0
Grab each edition of Mag Pi as soon as possible!

HOW TO...

How to boot Raspberry Pi 4 from USB

0
How to set up and boot Raspberry Pi 4 from USB drive - headless guide.

Raspberry Pi Zero, Raspberry Pi 3A+ WiFi without keyboard or cables

0
Getting Raspberry Pi boards connected in seconds

Everything you need to start using JSON

0
It's JSON not Jason, just saying!

How to use Join App in NodeRED

0
The day finally comes where Join app is almost everywhere!

Driving LCD with Arduino and Raspberry Pi

0
Displaying things on an LCD screen is as easy as going through a cup of jellybeans while writing this tutorial!