IP Watcher

What is it good for? I have a homepage and dynamic IP address that changes after an arbitrary timespan. When the change happens I can’t reach my server. To get it work again I have to adapt the IP location manually. I know there exists e.g. dyndns… or upgrade to a static IP but I want to save money and have no problems in doing it.

Here is a script that compares the current IP address with a stored one and sends the new by email to a user defined address:

#!/bin/bash
MYIP="/home/user/myip.txt"
GETIP4ADDR="wget -qO- http://ipecho.net/plain | xargs echo"
GETIP6ADDR="wget -qO - icanhazip.com"
IPADDR=$($GETIP4ADDR)
EMAIL="sendmail -f noreply@sender.com receiver@email.com"

if [ -f "$MYIP" ]; then  
  LASTIP="$(cat $MYIP)"
  if [ "$IPADDR" != "$LASTIP" ]; then
    # IP changed
    echo "$IPADDR" > $MYIP
    echo "subject: IP Update $IPADDR" | $($EMAIL)
  fi
else
  # file missing, create file
  echo "$IPADDR" > $MYIP
  echo "subject: IP Update $IPADDR" | $($EMAIL)
fi

Hints

  1. sendmail
    sudo apt install sendmail
    sudo sendmailconf
  2. Adapt
    MYIP file location
    sender email address
    receiver email address
  3. Add script to your cron jobs, e.g. twice a day