LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxQuestions.org Member Success Stories (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/)
-   -   Simple bash script to check current ip and email new ip if changed (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/simple-bash-script-to-check-current-ip-and-email-new-ip-if-changed-4175609054/)

TheEzekielProject 07-02-2017 10:31 PM

Simple bash script to check current ip and email new ip if changed
 
Hello all,
I've been creating various scripts to hone my scripting skills so I thought I'd share this here. It's just a simple bash script to check current ip address and, if it has changed, email the new current ip to specified address. You may need to create a couple empty files if they don't yet exist. (e.g. currentip.txt and oldip.txt) I may add in a test to check for existence and create if necessary at a later date.

Note that you will need to change /PATH/TO/SCRIPT to the proper location
Code:

#!/bin/bash

IP=$(curl ipinfo.io/ip)
DIFF=$(diff /PATH/TO/SCRIPT/currentip.txt <(echo "$IP"))
if [ "$IP" ]
then

    if [ "$DIFF" = "" ]
    then
        :
    elif [ "$DIFF" != "" ]
    then
        mv /PATH/TO/SCRIPT/currentip.txt /PATH/TO/SCRIPT/oldip.txt       
        echo "$IP" >/PATH/TO/SCRIPT/currentip.txt
        mail -s "SUBJECT" YOUREMAIL@EXAMPLE.COM < /PATH/TO/SCRIPT/currentip.txt
    fi
else   
    : 
fi

Any questions, edits/suggestions are welcome


All times are GMT -5. The time now is 04:51 PM.