mKit.cz

VŠCHT & Development notes

Uživatelské nástroje

Nástroje pro tento web


tips:send_email_when_your_public_ip_address_has_changed

Send email when your public IP address has changed

/usr/local/sbin/check-public-ip-address

check-public-ip-address
#!/bin/bash
#
# check-public-ip-address
#
# script to check public IP address via IP test API Providers
# compare it to dns record and in case of change send it to given email
#
# (IP test API Provider has to return your public IP address 
# over http protocol in plain text)
#
###############################################################
 
# CONFIG
EMAIL=mail@example.com
DNSNAME="host.example.com"
TIME=`date`;
 
###############################################################
 
# IP test API Providers
API_PROVIDERS=(
"http://api.ipify.org"
"http://myexternalip.com/raw"
)
MAX_INDEX=$(( ${#API_PROVIDERS[@]} - 1))
 
# Is there at least one IP test API Provider?
if [ "$MAX_INDEX" -lt "0" ]
then
        echo "No API URLs test sites available!"
        exit 1
fi
 
# get random API URL
INDEX=`shuf -i 0-$MAX_INDEX -n 1`
URL=${API_PROVIDERS[$INDEX]}
 
# get public IP from provider
IP=$(curl -s $URL)
 
# get old ip from dns record
LASTIP=`dig +short $DNSNAME`
 
# compare
if [[ ${IP} != ${LASTIP} ]]
then
        echo "New IP = ${IP}"
        echo "sending email.."
        echo -e "Your IP addres has changed!\n\nDNS name  = $DNSNAME\nTimestamp = ${TIME}\n\nNew IP    = ${IP}\nDNS IP    = $LASTIP\n\nTEST API  = $URL\n\nBye" | \
                mail -s "[PUBLIC IP] New IP $DNSNAME" $EMAIL;
else
        echo "no IP change!"
fi

add exec permission

chmod +x /usr/local/sbin/check-public-ip-address

add cron task to check every 10 minutes
/etc/cron.d/check-public-ip-address

*/10 * * * * user /usr/local/sbin/check-public-ip-address > /dev/null
tips/send_email_when_your_public_ip_address_has_changed.txt · Poslední úprava: 2015/04/10 23:18 (upraveno mimo DokuWiki)