Raspberry Pi をモニタキーボードを接続しないで運用しようとすると、SSH接続するにもVNC接続するにもIPアドレスがわかったほうが便利です。IPアドレスをメール通知する設定を行います。
Macの場合は、設定するとファインダーに表示される設定も可能ですが、それはまた別の機会にまとめたいと思います。
sudo apt install ssmtp
インストールが完了したら、設定ファイルが /etc/ssmtp/ssmtp.conf に書かれています。
# # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=xxxxxxxx@gmail.com # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=smtp.gmail.com:587 # Where will the mail seem to come from? rewriteDomain=gmail.com # The full hostname hostname=gmail.com AuthUser=xxxxxxxx@gmail.com AuthPass=XXXXXXXX AuthMethod=LOGIN UseSTARTTLS=YES # Are users allowed to set their own From: address? # YES - Allow the user to specify their own From: address # NO - Use the system generated From: address FromLineOverride=YES
自分のgmail の設定に書き換えて保存。
メールスクリプトを作成
vi /home/pi/sendip.sh
#!/bin/sh
echo "start sleep..."
sleep 30s
echo "creating email..."
mailTo="yyyyyyyy@gmail.com"
mailFrom="xxxxxxxx@gmail.com"
mailSubject="Raspberry Piが起動しました"
echo "To:${mailTo}" > mailFile
echo "From:${mailFrom}" >> mailFile
echo "Subject:${mailSubject}" >> mailFile
echo "" >> mailFile
var=`ifconfig wlan0`
IFS_ORG="$IFS"
IFS=" "
set -- $var
var="$7"
IFS=":"
set -- $var
var="$2"
IFS="$IFS_ORG"
echo "$var" >> mailFile
sudo sendmail -t < mailFile
echo "sent email..."
cat mailFile
rm mailFile
exit 0
/etc/rc.local にシェルスクリプト実行を記述します。
<code>vi /etc/rc.local</code>
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi /home/pi/sendip.sh exit 0
再起動してメールが到達するか確認しておきます。
参考/出典

コメント