Update Ip

The ISP is changing the router IP on a regular basis every night.
Without DynDNS the IP adress is unknown after the IP address has changed.

One way to know the updated address is to use a script that gets the actual IP, and if it changes it sends an email with the new IP to my mailbox.

1) The file that stores the used IP address

echo 1.1.1.1 > IP.txt

2) The script (ip_mail) that reads the actual IP address, and if it changes, stores to the file "IP.txt" and sends out an email

#!/bin/bash

#get previously stored IP-address of router
IP_old=$(cat /home/debian/ip_mail/IP.txt)
#get actual IP of router
IP_new=$(curl http://my.ip.fi)
echo "IP_old $IP_old"
echo "IP_new  $IP_new"

if [ "$IP_new" = "$IP_old" ]
then
  echo "IP is the same"
else
 echo "IP is new"
 echo $IP_new > /home/debian/ip_mail/IP.txt
 echo "$IP_new" | mail -s "IP: $IP_new" mail@weissholger.de
fi

3) Edit the crontab ("crontab -e") for root to check IP every 3 minutes

#set path to find commands
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

#disable the mail alert
MAILTO=""

# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
*/3 * * * *  /home/debian/ip_mail/ip_mail
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License