Get email upon change of IP address

Setup a cron script to email upon change (checked each hour) in case your PC IP address changes (public or private). It uses sendmail to setup an email server on your PC.

sudo apt install sendmail

> ~/.current_ip

nano ~/scripts/checkIP

and cut and paste (change [email protected] to your email address)\

#!/bin/bash

YOUREMAIL="[email protected]"

IPADDRESS=$(hostname -I | tr -d [:space:])
OldIP=$(<~/.current_ip)

if [[ ${IPADDRESS} != ${OldIP} ]]; then
echo "Your new IP address is ${IPADDRESS} (old address was ${OldIP} )" | mail -s "IP address change" $YOUREMAIL
echo ${IPADDRESS} > ~/.current_ip
fi

Then save, exit nano and type

chmod +x ~/scripts/checkIP

crontab -e

and then at the top add:

SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin

and at the bottom add:

0 *  *   *   *     ~/scripts/checkIP

Reference

Tags:

Categories:

Updated: