Skip to content

Instantly share code, notes, and snippets.

@danielharrelson
Last active January 16, 2017 16:26
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 danielharrelson/b4b17aef643b68740e10a61e2667033a to your computer and use it in GitHub Desktop.
Save danielharrelson/b4b17aef643b68740e10a61e2667033a to your computer and use it in GitHub Desktop.
Monitor Raspberry Pi IP
#!/bin/bash
# Last Update: 2017/01/16
#
# Monitor for wireless IP change, if it changes, text your phone (or you could use an email)
# You will have to use your carrier text message gateway email address, example : yournumber@vtext.com
# Find your gateway at http://www.digitaltrends.com/mobile/how-to-send-e-mail-to-sms-text/
#
# Useful for instances when you are using a headless Pi and it may/may not be portable.
#
# You will have to preconfigure /etc/ssmtp/ssmtp.conf to have this working.
#
# Recommend adding this to the crontab with a line like the following
# */3 * * * * /home/pi/network-change.sh > /dev/null 2>&1
#
# Fill out the appropriate variables below
EMAILADDRESS="" # your email goes here
INTERFACE="" # example wlan0
# Check for file existance
# If File doesn't exist, it will create it and then send out an email/text
if [ ! -f "/tmp/wireless-ip.txt" ];
then
/sbin/ip addr show $INTERFACE | /bin/grep -E -e 'inet[ ]' | /usr/bin/awk '{print $2}' > /tmp/wireless-ip.txt;chown pi:pi /tmp/wireless-ip.txt
/sbin/ip addr show $INTERFACE | /bin/grep -E -e 'inet[ ]' | /usr/bin/awk '{print $2}' | /usr/sbin/ssmtp $EMAILADDRESS
fi
/sbin/ip addr show $INTERFACE | /bin/grep -E -e 'inet[ ]' | /usr/bin/awk '{print $2}' | /bin/grep -f /tmp/wireless-ip.txt
# Code that compares the existing value(s) of the ip address vs the former value(s) stored within the /tmp/wireless-ip.txt
if [ $? -ne 0 ]
then
/sbin/ip addr show $INTERFACE | /bin/grep -E -e 'inet[ ]' | /usr/bin/awk '{print $2}' > /tmp/wireless-ip.txt
/sbin/ip addr show $INTERFACE | /bin/grep -E -e 'inet[ ]' | /usr/bin/awk '{print $2}' | /usr/sbin/ssmtp $EMAILADDRESS
exit 0
else
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment