Skip to content

Instantly share code, notes, and snippets.

Last active November 12, 2021 20:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
# Raspberry Pi Pushover IP Notifier
# by Matt Harzewski (2021)
# License: MIT
# On system boot, this script checks to see if
# the IP address has changed, and sends a
# Pushover notification if it is different.
# Set the user and application tokens with your
# own from, and add the script to
# the crontab: @reboot python3 path/to/
from urllib import request, parse
from datetime import datetime, date
import socket, os.path, subprocess
def push(title, msg):
params = {
"title": title,
"message": msg
req = request.Request(
res = request.urlopen(req)
def getIp():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip = None
sock.connect(('', 1))
ip = sock.getsockname()[0]
except Exception:
return ip
def storeIp(ip):
with open(os.path.expanduser('~/.ipnotify'), "w+") as f:
def getStoredIp():
ip = None
when = None
with open(os.path.expanduser('~/.ipnotify'), "r") as f:
data =
data = data.split('|')
ip = data[0]
when = data[1]
return ip, when
def getUptime():
return subprocess.getoutput('uptime -s')
if __name__ == '__main__':
current = getIp();
today = datetime.combine(, datetime.min.time())
if os.path.exists(os.path.expanduser('~/.ipnotify')):
prev, when = getStoredIp()
dt = datetime.strptime(when.strip(), "%Y-%m-%d %H:%M:%S")
if current != prev or dt < today:
push("Current IP", current)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment