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.

  1. install email daemon

     apt install sendmail
    
     > ~/.current_ip
    
     nano ~/scripts/checkIP
    
  2. create sendmsg.sh (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
    
  3. setup cron

     chmod +x ~/scripts/checkIP
    
     crontab -e
    

    adding at the top:

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

    at the bottom add:

     0 *  *   *   *     ~/scripts/checkIP
    

Reference

Tags:

Categories:

Updated:

Leave a Comment