Skip to content

Instantly share code, notes, and snippets.

@rogersguedes
Created October 15, 2019 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogersguedes/6e592cd05cede2ca66b9ccc31e2e206c to your computer and use it in GitHub Desktop.
Save rogersguedes/6e592cd05cede2ca66b9ccc31e2e206c to your computer and use it in GitHub Desktop.
This script check your public IP on checkip.dyndns.org and logs its changes
#/bin/bash
LOGFILE_NAME=whatismyip.log
GATEWAY_IP=192.168.0.1
IP_PAGE_URL=checkip.dyndns.org
LAST_IP=`curl -s ${IP_PAGE_URL} | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
echo "Initial IP: ${LAST_IP}"
while true
do
sleep 2
CURR_TIME=`date '+%Y-%m-%d.%H.%M.%S'`
echo "."
CURR_IP_HTML=`curl -s ${IP_PAGE_URL}`
if [ $? -ne 0 ]; then
echo "[${CURR_TIME}] Couldn't connect to ${IP_PAGE_URL}" >> ${LOGFILE_NAME}
GT_PING_STDOUT=`ping -c 1 ${GATEWAY_IP} 2>/dev/null`
if [ $? -ne 0 ]; then
echo "[${CURR_TIME}] Couldn't reach LAN Gateway (${GATEWAY_IP})" >> ${LOGFILE_NAME}
continue
fi
fi
CURR_IP=`echo ${CURR_IP_HTML} | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
if [ ${CURR_IP} != ${LAST_IP} ]; then
echo "[${CURR_TIME}] IP changed, ${LAST_IP} => ${CURR_IP}" >> ${LOGFILE_NAME}
fi
LAST_IP=${CURR_IP}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment