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