I wrote this little shell script and thought I'd share it since it might come in handy to others as well. Simply put, it e-mails the external IP address to you if it has changed since the last time the script was run. I've only tried this with Gmail, but you're free to modify the script to suit your needs.
How to use:
First, you must update your package list and install mailutils and ssmtp:
Then, open /etc/ssmtp/ssmtp.conf in a text editor (I used Nano)sudo apt-get update
sudo apt-get install mailutils ssmtp
Edit the mailhub line so that it looks like this:sudo nano /etc/ssmtp/ssmtp.conf
and add these to the end of the file (replace the AuthUser and AuthPass values with your gmail address and password):mailhub=smtp.gmail.com:587
Save the file. Then open /etc/ssmtp/revaliases in a text editor...AuthUseremail@example.com
...and add this to the bottom of the file, again replacing "yourgmailaddress" with your own Gmail login:sudo nano /etc/ssmtp/revaliases
All that is needed now is the script itself.root:firstname.lastname@example.org:smtp.gmail.com:587
A quick explanation on how it works: When you run it for the first time, it retrieves your external IP address and stores it in a file called "ip.txt". This file will be created in the same folder as the script. Then, if the script is run again, it will load the previous IP address from the file, and will also check your current address. If these two match, i.e. the address has not changed, the script will just exit. If they do not match (the address has changed), the new IP address will be written to the text file (overwriting the old one) and emailed to you. Then the script will exit.
Put the subject of the message between the quotes in SUBJ, and the email address of the recipient between the quotes in EMAIL.
Code: Select all
#!/bin/sh SUBJ="" EMAIL="" ip1="" ip2="" read ip1 < ip.txt ip2=$(wget -qO- ifconfig.me/ip) if [ "$ip1" = "$ip2" ] then exit else echo "$ip2" > ip.txt echo "$ip2" | mail -s $SUBJ $EMAIL exit fi
Now, the part where we make it run automatically. We're going to use Cron, which is a program you can use to schedule things. This should be installed by default.
First, open the Cron configuration file:
Then add this line at the bottom:sudo crontab -e
I will not go into detail on what the different values do - there are plenty of tutorials and manuals online. The above line runs the script every 15 minutes, 24/7. It will only send the email message if the IP address has changed. As for the &>/dev/null part at the end - don't ask me what it is or how it works. All I know is that it stops Cron from sending useless error reports and other things to your email0,15,30,45 * * * * sh *full path to script here*/*scriptnamehere*.sh &>/dev/null
That is all. This should be useful in case you regularly connect to your RasPi from outside your local network. Feel free to post here if you have any questions and I can at least try to help