Raspberry Pi

Send an E-mail when LAN or WAN IP changes

My Raspberry Pi is WI-FI DHCP behind the WAN facing firewall. I wanted it to send me an e-mail every time its IP or WAN IP of the firewall change and below is what I came up with. I have used the automation API of whatismyip.com to determine the WAN IP of the firewall and the output of /bin/hostname -I

set -e
set -u

read newlanip < <(/bin/hostname -I)
newwanip=$(/usr/bin/curl -s -m20 -A 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0' http://automation.whatismyip.com/n09230945.asp)

echo "new IPs: [$newlanip] [$newwanip]"

if [[ ! -s ${0%.sh}.myip ]]; then
  echo "" > ${0%.sh}.myip
  echo "no saved IP, generated ${0%.sh}.myip"

read oldlanip oldwanip < ${0%.sh}.myip
echo "old IPs: [$oldlanip] [$oldwanip]"

echo "$newlanip $newwanip" > ${0%.sh}.myip
if [[ $newlanip != $oldlanip || $newwanip != $oldwanip ]]; then
  echo "IP changed: [$newlanip] [$newwanip]"
  mail -s "My IPs: LAN:[$newlanip] WAN:[$newwanip]" mail'@'bobah.net < /dev/null