HomeRaspberry PiRapsberry 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 with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

M5Paper

Programable, ESP32 based awesome dev platform with 4.7 e-ink display by M5Stack

More HATs

client-image
client-image

Argon One M.2

Enclose Raspberry Pi 4 inside this great case with custom I/O, cooling and GPIO and M.2 SSD support

More cases on

client-image
client-image

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.

Slow Internet Warning

0
From time to time my Internet grinds to a stop. Since Raspberry Pi 4 comes with a 1Gbps Ethernet, I decided to take advantage of it and create a reporting system in NodeRED that will monitor and report when the ISP is not keeping the contractual agreements. Works with Alexa, Google Home, Android and Windows 10.

How fast Raspberry Pi NAS is?

0
Let's see how fast Raspberry Pi NAS really is?

Argon18: Argon ONE SSD modification

0
Argon One case just got better - now you can boot it from USB without ruining the design thanks to Argon 18: Argon One SSD modification

HOW TO...

It took me 2 months to boot CM4 from NVMe

0
Complete beginners guide to Compute Module 4 boot from NVMe.

Raspberry Pi Zero 2 W vs other Zero boards

0
It's time to test the Raspberry Pi Zero 2 W against other Raspberry Pi boards from Zero series: power, WiFi, temperature and core performance

C/C++ and MicroPython SDK for Raspberry Pi Pico on Windows

0
A guide to SDK toolchain for Raspberry Pi Pico and C/C++ , Micropython on Windows.

A comprehensive guide to Grafana & InfluxDB

0
How to use Grafana and InfluxDB on Raspberry Pi for IoT sensors in home automation

How to boot Raspberry Pi 4 from USB

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