由于我使用树莓派的场景大多数是在没有显示器、只用terminal连接它的情况下,所以,它的IP地址有时会在重启之后变掉(DHCP的),导致我无法通过terminal连接上它。然后我又要很麻烦地登录路由器的管理界面里,去看它被分配到的新IP是什么,然后用terminal重连,太麻烦了,不是么?作为一个树莓派玩家,这种麻烦简直是无法接受的!

为了解决这个问题,我让Pi开机的时候,自动向我指定的Email发送一封邮件,告诉我它此次开机时的IP地址。
步骤: 开机时执行一个脚本,检测网络可用性→网络通畅后获取自己的IP地址→发送邮件到指定的邮箱。
下面一一道来。

1、开机启动项
开机执行一个脚本是怎么做到的?
只需要向 /etc/rc.local 文件中添加一句话,即可开机执行一个脚本了:

1
2
# send a mail to notify the IP address of Pi
/root/data/source/send-ip-mail.sh >> /root/data/source/send-ip-mail.log 2>&1

2、上报IP地址的脚本实现
send-ip-mail.sh脚本的内容如下:(vim不会自动创建指定目录)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
  
# check network availability
while true
do
  TIMEOUT=5
  SITE_TO_CHECK="www.126.com"
  RET_CODE=`curl ----connect-timeout $TIMEOUT $SITE_TO_CHECK -%{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 -"2,2p" | awk '{print substr($2,1)}'`
  
# send the Email
echo "Current time: `date '+%F %T'`. Enjoy it" | mutt -"IP Address of Raspberry Pi: $ETH0_IP_ADDR" xxx@gmail.com

脚本很简单,分为3部分:第一部分检测网络可用性;第二部分取树莓派的eth0网卡的IP地址;第三部分发送邮件到指定的Email。
其中,第一部分是必须要有的,因为经过我试验,在本脚本执行时,树莓派的网络还没有初始化好,此时你直接发邮件是发不出去的。在这里我通过访问www.126.com来确定网络可用性。
第三部分需要你预先配置好mutt和msmtp。

3、安装配置mutt和msmtp
配置好mutt和msmtp后,就可以像上面一样,通过一句代码将邮件发送出去。
首先要在Pi上安装mutt和msmtp:

1
2
pacman -S msmtp
pacman -S mutt

安装后,先配置msmtp。在你用户的根目录下创建文件 .msmtprc,内容如下:

1
2
3
4
5
6
7
account default
host smtp.126.com
from xxx@126.com
auth plain
user xxx@126.com
password your_password
logfile /var/log/msmtp.log

其中,smtp.126.com是我使用的邮箱的SMTP服务器地址,xxx@126.com是我用于发送邮件的邮箱,your_password是邮箱密码,你要根据你的情况修改。

然后配置mutt。在你用户的根目录下创建文件 .muttrc,内容如下:

1
2
3
4
set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="Alarm"
set editor="vim"

其中,realname是发件人的名字,接收到的邮件中会显示出来。

4、msmtp测试

1
2
测试配置文件:msmtp -P
测试smtp服务器:msmtp -S
1
2
3
4
5
6
7
8
9
10
11
12
bitnami@linux:~$ msmtp --host=smtp.163.com --serverinfo
SMTP server at smtp.163.com (smtp.163.gslb.netease.com [220.181.12.18]), port 25:
    163.com Anti-spam GT for Coremail System (163com[20121016])
Capabilities:
    PIPELINING:
        Support for command grouping for faster transmission
    STARTTLS:
        Support for TLS encryption via the STARTTLS command
    AUTH:
        Supported authentication methods:
        PLAIN LOGIN
This server might advertise more or other capabilities when TLS is active.

从返回信息中我们可以看到,这个smtp是支持TLS的,验证方式支持 PLAIN 和 LOGIN

5、测试邮件

命令行输入:

1
echo "test" |mutt -"my_first_test" aaa@126.com

6、至此全部搞定,以后每次Pi开机的时候,就会“自报家门”,我们再也不愁找不到Pi啦!

7、常见问题:

错误1:

msmtp: account default not found: no configuration file available
msmtp有bug,必须手动指定对应的配置文件
更改/etc/Muttrc中set sendmail="/usr/bin/msmtp"为set sendmail="/usr/bin/msmtp -C .msmtprc"
错误2:

msmtp: GNU SASL: Base 64 coding error in SASL library
遇到Base64 编码错误
更改~/.msmtprc中auth login
为 auth plain
错误3:

语句:echo "testtest"|mutt -F/home/spider/.muttrc -s "tttttttt" test@163.com
发邮件时提示:寄送讯息出现在错误,子程序已结束 127 (Exec error.).
无法寄出信件

一般是设置文件出现问题了,

先使用msmtp进行发送测试

1
2
3
4
5
6
7
8
9
10
[iyunv@zabbix ~]# /usr/local/msmtp/bin/msmtp -S
SMTP server at smtp.sohu.com ([220.181.90.34]), port 25:
    zw_71_37 ESMTP ready
Capabilities:
    STARTTLS:
        Support for TLS encryption via the STARTTLS command
    AUTH:
        Supported authentication methods:
        PLAIN LOGIN
This server might advertise more or other capabilities when TLS is active.

发现没有问题

再利用msmtp查看当前文件路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[iyunv@zabbix ~]# /usr/local/msmtp/bin/msmtp -P
loaded system configuration file /usr/local/msmtp/etc/msmtprc
ignoring user configuration file /root/.msmtprc: No such file or directory
falling back to default account
using account default from /usr/local/msmtp/etc/msmtprc
host                  = smtp.sohu.com
port                  = 25
timeout               = off
protocol              = smtp
domain                = localhost
auth                  = LOGIN
user                  = zabbix2018
password              = *
passwordeval          = (not set)
ntlmdomain            = (not set)
tls                   = off
tls_starttls          = on
tls_trust_file        = (not set)
tls_crl_file          = (not set)
tls_fingerprint       = (not set)
tls_key_file          = (not set)
tls_cert_file         = (not set)
tls_certcheck         = on
tls_force_sslv3       = off
tls_min_dh_prime_bits = (not set)
tls_priorities        = (not set)
auto_from             = off
maildomain            = (not set)
from                  = zabbix2018@sohu.com
dsn_notify            = (not set)
dsn_return            = (not set)
keepbcc               = off
logfile               = /var/log/zabbix/msmtp.log
syslog                = (not set)
aliases               = (not set)
reading recipients from the command line

从上面显示配置文件也没有什么问题,但是查看.muttrc时同时注意到双引号字符错误。修改键盘布局。

错误4:

ding@ubuntu:~/Desktop/python$ sudo echo hello world | mutt -s "test mail" XXXXXXX@qq.com
msmtp: authentication failed (method PLAIN)
msmtp: server message: 550 User is locked
msmtp: could not send mail (account default from /home/ding/.msmtprc)
Error sending message, child exited 77 (Insufficient permission.).
Could not send the message.

没有开启SMTP服务,新注册的用户默认好像是关闭的,一些邮箱是默认关闭smtp服务的,需要手动开启。

开启SMTP服务后,将163邮箱服务器发给的授权密码作为/home/ding/.msmtprc 文件中的password=授权码

参考:http://jingyan.baidu.com/article/3f16e003e327772591c1039f.html?st=2&os=0&bd_page_type=1&net_type=2

错误5:

str0101@str0101:/u1/str0101>mutt -s dfdf zgq@mail.tm <.tmp
Error sending message, child exited 69 (Service unavailable.).
Segmentation fault (core dumped)

邮件服务器限制,查看sent日志文件。(我由QQ更换为网易邮箱)

扩展:

使用标准pc104键盘

国内多使用标准104键盘,下面就开始树莓派的设置。

1、sudo raspi-config

2、进入国际化配置选项

3、修改键盘布局

4、选择PC104标准键盘

5、选择美国标准

6、选择键盘默认布局

7、compose key设置

8、ctrl+alt+backspace组合键,类似于windows的ctrl+alt+delete。

9、完成设置

Raspberry Pi开发之旅-发送邮件记录时间及IP的更多相关文章

  1. Raspberry Pi开发之旅-同步时间

    使用htpdate同步时间 由于树莓派板子上没有 RTC 硬件和电池,因此树莓派上的系统时间重启是保存不了的.网上已经有人想到应对 NTP 被防火墙封掉类似的需求了,开源的 htpdate 命令直接使 ...

  2. Raspberry Pi开发之旅-实现云平台监控

    一.基本设置 1 sudo raspi-config 移动到第五项“Enable Camera”,回车进入,按tab键切换到“Enable”回车确认.回到主菜单,tab键切换到“Finish”回车确认 ...

  3. Raspberry Pi开发之旅-土壤湿度检测

    一.土壤传感器 传感器四个针脚:  针脚 含义 AO 模拟信号输出 DO 数字信号输出 GND 电源负极 VCC 电源正极 二.接线 YL-38和YL69 之间直接用2根母对母线连接. YL-38和树 ...

  4. Raspberry Pi开发之旅-WIFI遥控小车

    一.简单介绍树莓派的GPIO口 上图是树莓派2代的接口(不同型号接口会有差异),我们就以此为例来说下这些接口. 1.GPIO介绍 GPIO 英文全称是:General-purpose input/ou ...

  5. Raspberry Pi开发之旅-控制蜂鸣器演奏乐曲

    一.无源蜂鸣器和有源蜂鸣器 步进电机以及无源蜂鸣器这些都需要脉冲信号才能够驱动,这次尝试用GPIO的PWM接口驱动无源蜂鸣器弹奏一曲<一闪一闪亮晶晶>. 无源蜂鸣器: 无源内部没有震荡源, ...

  6. Raspberry Pi开发之旅-光照强度检测(BH1750)

    一.前期准备 1.环境要求 GY30模块(BH1750FVI传感器),树莓派系统,python-smbus,iic开启 2.取消对IIC驱动的黑名单 nano /etc/modprobe.d/rasp ...

  7. Raspberry Pi开发之旅-远程监控

    1.安装辅助工具 1 2 sudo apt-get install libjpeg8-dev sudo apt-get install cmake 2.编辑源文件 1 2 sudo git clone ...

  8. Raspberry Pi开发之旅-空气温湿度检测(DHT11)

    一.首先,简单介绍下DHT11: DHT11是一个温湿度传感器,分为3个接口,分别为:VCC, DATA, GND  引脚号 名称 类型 说明 1 VCC 电源 +级,输入3V-5.5V 2 DATA ...

  9. [树莓派(raspberry pi)] 02、PI3安装openCV开发环境做图像识别(详细版)

    前言 上一篇我们讲了在linux环境下给树莓派安装系统及入门各种资料 ,今天我们更进一步,尝试在PI3上安装openCV开发环境. 博主在做的过程中主要参考一个国外小哥的文章(见最后链接1),不过其教 ...

随机推荐

  1. abp 列表和明细页面的规则说明

    0>列表区域mainviewbody 1>列表头mainviewheader 名称 2>列表条件mainviewcodtionbody 条件区 3>列表工具栏mainviewt ...

  2. Android Studio--NDK编译C代码为.so文件,JNI调用

    前言: 从Android Studio开始,就支持jni和.so库调用了. 环境: Windows 7+Android Studio2.1.2+NDK版本:android-ndk-r10e 准备工作: ...

  3. Hibernate生成器类

    在Hibernate中,id元素的<generator>子元素用于生成持久化类的对象的唯一标识符. Hibernate框架中定义了许多生成器类. 所有的生成器类都实现了org.hibern ...

  4. SlidingMenu——使用前的配置

    一: 首先下载lib:SlidingMenu.然后将起导入eclipse中,然后将其clean一下,重新生成R文件. 二: 因为SlidingMenu依赖ActionBarSherlock,所以需要下 ...

  5. 关闭QQ看点

    手机qq联系人 然后右上角公众号 然后看到看点 取消关注!!!

  6. poj 2226(最小覆盖)

    题目链接:http://poj.org/problem?id=2226 思路:将连续的横向水洼看成X集合中的一个点,连续的纵向水洼看成Y集合中的一个点,而将每个水点看成一条边,它连接了所在的X集合中的 ...

  7. 七个迹象说明你可能受到APT 攻击

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXF1c2hp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  8. X明X源面试题《三》

    本文转自自zhangxh_Doris 昨天(05.23)下午去参加了明源软件的暑期实习宣讲+笔试,第一次听说这个行业,行业和笔试风格完全不一样啊,5道行测智力题+1个问答+ 斐波那契数列 + 洗牌算法 ...

  9. LeetCode Problem 136:Single Number

    描述:Given an array of integers, every element appears twice except for one. Find that single one. Not ...

  10. L - Sum It Up(DFS)

    L - Sum It Up Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Descr ...