Skip to content

Instantly share code, notes, and snippets.

@csawall
Created April 30, 2017 14:49
Show Gist options
  • Save csawall/ce879b371cb15ab725c50448479bf0cc to your computer and use it in GitHub Desktop.
Save csawall/ce879b371cb15ab725c50448479bf0cc to your computer and use it in GitHub Desktop.
Check external IP and alert on change
#!/bin/bash
#set debug value to "true" to output debug code
debug=false
SlackURL_key="https://hooks.slack.com/services/KEYDATA01/KEYDATA02/KEYDATA03"
SlackUsername="icanhazip"
#obtain current IP from icanhazip
myip=`curl -4 -s icanhazip.com`
if [ $debug == "true" ]; then echo "current IP = " $myip; fi
#check for existing temp file to compare last known IP
if [ -e /tmp/myip.txt ]
then
tmpip=`cat /tmp/myip.txt`
echo "temp IP = " $tmpip
if [ $myip == $tmpip ]
then
if [ $debug == "true" ]; then echo "good"; fi
else
#if the IP does not match, sent a Slack alert!
Slackjson="{\"text\":\"External IP Changed!\nOld IP = ${tmpip}\nCurrent IP = ${myip}\", \"username\":\"${SlackUsername}\"}"
if [ $debug == "true" ]; then echo "nogood"; fi
fi
#if no current temp file, create a new one with the current IP
else
echo $myip > /tmp/myip.txt
#if no temp file, send notice that there was nothing to compare against
Slackjson="{\"text\":\"No temp file to compare against!\nCurrent IP = ${myip}\", \"username\":\"${SlackUsername}\"}"
if [ $debug == "true" ]; then echo "temp file did not exist, creating a new one"; fi
fi
#check if the Slackjson variable has been set and if so, send to Slack
if [ -n "${Slackjson}" ]
then
curl --silent --request POST --header "Content-Type: application/json" --data "${Slackjson}" ${SlackURL_key} > /dev/null
if [ $debug == "true" ]; then echo "sending to Slack"; fi
else
if [ $debug == "true" ]; then echo "no Slack json variable set"; fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment