Raspberry Pi boot IP address to send mail

Raspberry Pi using a wifi network, used in the hostel is a public network, you can not set a static ip, raspberries come in every time after power is turned on ip addresses can change, so let Raspberry Pi turned on automatically sent to the QQ network ip address mailbox

First, install mutt and msmtp

mutt: Mail is a program under Linux, Mutt is more like a file manager, but it is the management of email.

msmtp: Linux is a very easy to use smtp client.

sudo apt-get install mutt
sudo apt-get install msmtp

Second, configure mutt and msmtp

1, the configuration msmtp, .msmtprc new file in the root directory of the user, as follows:

default the Account 
Host smtp.aliyun.com 
from your e-mail address 
auth Plain 
the User your email address 
password your mailbox password 
logfile /var/log/msmtp.log
  • host your mailbox SMTP server address, e-mail can be found on the website, I use Ali cloud-based mailbox, so the suffix is aliyun.com
  • from here is the address of the sender, the recipient will be displayed, set your e-mail address to send the application raspberry
  • user here is the user name, users say the Internet has set up here in front of the mailbox username @ symbol strings on it, I did not try, my email address is set, it is recommended to use the e-mail address, after successful configuration can conduct their own modify
  • password this is their mailbox passwords, nothing to say, but here is the plain text password, pay attention to the confidentiality :)
  • logfile This is msmtp program run log file path, you need to create your own, he will not create msmtp The program will send written to the log file in the path after sending the message.

2, the configuration file mutt, also in the new document root .muttrc user, as follows:

set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="RaspberryPi"
set editor="vim"

 Use the following command to test whether to send mail success:

echo “hello world” | mutt -s “title” [email protected]

Second, set the Raspberry Pi Ip address read and send by e-mail

The following script file referenced article send-ip-mail.sh online

#!/bin/bash
# check network availability 
while true
do
TIMEOUT=5
SITE_TO_CHECK="www.126.com"
RET_CODE=`curl -I -s --connect-timeout $TIMEOUT $SITE_TO_CHECK -w %{http_code} | tail -n1`
if [ "x$RET_CODE" = "x200" ]; then
echo "Network OK, will send mail..."
break
else
echo "Network not ready, wait..."
sleep 1s
fi
done
# get the IP address of eth0, e.g. "192.168.16.5" 
ETH0_IP_ADDR=`ifconfig eth0 | sed -n "2,2p" | awk '{print substr($2,1)}'`
# send the Email 
echo "Current time: `date '+%F %T'`. Enjoy it" | mutt -s "IP Address of Raspberry Pi: $ETH0_IP_ADDR" [email protected]

  

After adding execute permissions using chmod + x send-ip-mail.sh, run to see whether you can send ip address.

After the rc.local file, add the boot command to run the script, the power-on reset discovery can not receive mail, go online and read later found to be a permissions problem, after some agonizing, then select the .muttrc, .msmtprc and send-ip- mail.sh files are copied to the / boot directory, add the following in rc.local, and finally re-power can be distributed to raspberry received mail, login via normal e-mail address in the ip ssh.

 

 

Reference article: https://www.jianshu.com/p/893352c61f93

 



Guess you like

Origin www.cnblogs.com/lw77/p/11735184.html