I’m sure this has been done (and posted) before, but here goes anyway:
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:
先安装个ssmtp。First, you must update your package list and install mailutils and ssmtp:
sudo apt-get update sudo apt-get install mailutils ssmtp
新安装的ssmtp的配置文件在/etc/ssmtp下,Then, open /etc/ssmtp/ssmtp.conf in a text editor (I used Vim) ,用vim或者nano编辑:(英文全文在这里,使用gmail,为了适应国内需要,我改为126的网站)
sudo vim /etc/ssmtp/ssmtp.conf //后来才知道ssmtp已经不行了
配置mailhub告知邮件去哪里,Edit the mailhub line so that it looks like this:
mailhub=smtp.gmail.com:587 //原文是gmail的smtp mailhub=smtp.126.com //不知道126的端口是25,这里不放,看能不能行
and add these to the end of the file (replace the AuthUser and AuthPass values with your gmail address and password):
AuthUser=siemen@gmail.com AuthPass=wodemima UseSTARTTLS=YES AuthUser=iking@126.com AuthPass=wodemima UseSTARTTLS=YES
Save the file. Then open /etc/ssmtp/revaliases in a text editor…
sudo vim /etc/ssmtp/revaliases
…and add this to the bottom of the file, again replacing “yourgmailaddress” with your own Gmail login:
root:siemen.zem@gmail.com:smtp.gmail.com:587 root:iking@126.com:smtp.126.com:25 //对126的邮箱进行root的配置 pi:iking@126.com:smtp.126.com:25 //对pi进行配置
All that is needed now is the script itself.
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="RaspiUSB3_163_PubIP" EMAIL="Mygmail@gmail.com" 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
Save the file. You can test it by running “sh *scriptnamehere*.sh”. It will show an error on the first start as it is trying to load data from a file that doesn’t exist. You can safely ignore this. You should receive an e-mail containing your external IP address. The script has also created the “ip.txt” file. If you open it, you can see that it contains your “old” IP address.
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:
sudo crontab -eThen add this line at the bottom:
0,15,30,45 * * * * sh *full path to script here*/*scriptnamehere*.sh &>/dev/null
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 email

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

还是遇到一大堆问题,准备看这个
http://xiaqunfeng.cc/2017/01/11/ubuntu-email-send/
实际操作,我把sh文件稍微修改了一下,因为ssmtp已经过时了,我用的是mutt管理下的msmtp发送邮件
SUBJ="YourNewIPis:" EMAIL="zeng.qq@outlook.com" ip1="" ip2="" read ip1 </home/pi/ip.txt ip2=$(wget -qO- ifconfig.me/ip) if [ "$ip1" = "$ip2" ] then exit else echo "$ip2" >/home/pi/ip.txt #echo “$ip2” | mutt -s “The New IP here” siemen.zem@gmail.com echo "you get a new IP" | mutt -s "New IP address" siemen.zem@$ exit fi