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 请注意,脚本中的/PATH/TO/SCRIPT要换成你自己存放的位置,我currentip.txt存在/home/pi/Documents (或者直接用~指向“当前用户pi的Documents文件目录下即~/Documents/currentip.txt)
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
Oh yeah, before you can use the mail function, you have to install a mail utility example ”mail”
Sudo apt-get install mailutils
it will take some time to install after you type ”y” to allow the installation
some original website info for how to mail with CLI is here, it’s not easy to send as you still have to setup something which is not addressed in the tuition