Script to send a SMS when IP changes

So this is one application of the SMS gateway. My home internet gets its address via DHCP, however, unless something goes wrong, the addresses are usually reasonably persistent (generally at least 10 days).

I was motivated to set this up because when my ISP was having connectivity issues, it was very frustrating having to sit around and keep trying ping until it came back online.

Now I can just wait for a notification :)

I have this script running on my Ubiquti EdgeRouter but it would work the same if you had it on a workstation.

This goes in /etc/cron.hourly

#! /bin/bash

if [ ! -f ~/.public_ip_address ]; then                                                                                                                          
        ip  --brief addr | grep eth0.10 | awk '{print $3}' | sed s/'\/22'// 
        > ~/.public_ip_address                                                              

ip_addr_old=`tail -n1 ~/.public_ip_address`                                                                                                                     

if [ "$ip_addr_old" != "$ip_addr_new" ];                                                                                                                                
        echo $ip_addr_new >> ~/.public_ip_address
        sms_message="IP changed from $ip_addr_old to $ip_addr_new"

        curl -sss -u username:password http://raspi.local/sms/send-sms --data 
                <Date>$(date +"%Y-%m-%d %T")</Date>
         </request>" > /dev/null

s3cmd put ~/.public_ip_address s3://example-s3-bucket/

Here is a quick run through. We first check the file ~/.public_ip_address exists creating it if it doesn't and populating it with the current ip address. We compare the current ip as reported by curl against the latest entry in the file.

If the two do not match, then our IP has changed since we last checked. We update the file, and send a sms message via our sms gateway informing of the change.

We also push the latest version of the file to an s3 bucket