• 2023年亲测有效----树莓派启动时自动邮件上报ip


    首先开启qq邮箱smtp服务

    然后点击开启就会有授权码
    在这里插入图片描述

    shell文件内容

    在自己的shell里,运行echo $PATH,把内容覆盖下面的path。
    功能 作用就是 测试网络系统是否成功联通,命令ifconfig是否会有正常输出

    #!/bin/bash
    export PATH='/home/pi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/snap/bin'
    while true; do (echo `ifconfig|grep 192.168`)&& break;sleep 1;done;
    python3 /home/pi/rePortIp.py 
    
    • 1
    • 2
    • 3
    • 4

    设置raspi-config中的system options

    在这里插入图片描述
    然后选择 S6 ,Network at Boot
    Use this option to wait for a network connection before letting boot proceed.在这里插入图片描述
    等待网络子系统起来,这样就好挂载啊,发邮件咯

    启动自动执行

    主要工具:crontab

    crontab -e
    
    • 1

    添加末尾如下,ctrl + s 保存,然后ctrl + x退出

    @reboot /home/pi/reportIp.sh
    
    • 1

    python文件

    python文件就是邮件发送了

    # coding=utf-8
    import smtplib
    from email.mime.text import MIMEText
    from email.utils import formataddr
    import os
    import time
    
    #发送内容t
    def mail(t):
        ret=True
        try:
            msg=MIMEText(t,'plain','utf-8')
            msg['From']=formataddr(["Jack",my_sender])          # 括号里的对应发件人邮箱昵称、发件人邮箱账号
            msg['To']=formataddr(["Rose",my_user])              # 括号里的对应收件人邮箱昵称、收件人邮箱账号
            msg['Subject']="树莓派IP地址获取上报"                   # 邮件的主题,也可以说是标题
            server=smtplib.SMTP("smtp.qq.com", 587)             # 发件人邮箱中的SMTP服务器,端口是587
            server.login(my_sender, my_pass)                    # 括号中对应的是发件人邮箱账号、邮箱密码
            server.sendmail(my_sender,[my_user,],msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
            server.quit()  # 关闭连接
        except Exception:  # 如果 try 中的语句没有执行,则会执行下面的 ret=False
            ret=False
        return ret
    # 获取ifconfig命令内容
    
    cmd='ifconfig |grep 192.'
    
    
    t = "empty"
    while t == "empty":
        with os.popen(cmd,"r") as p:
            t = p.read()
            if not t:
                t = "empty"
        time.sleep(5)
    
            
    # 设置发件人和收件人信息
    my_sender='xxxx@xxx.com'  # 自己的邮箱账号
    my_pass = 'xxx'   # 发件人邮箱密码(之前获取的授权码)
    my_user=my_sender    # 自己的邮箱账号
    
    
    ret=mail(t)
    if ret:
        print("发送邮件成功")
    else:
        print("发送邮件失败")
    
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    注意事项

    1.确认开启了邮箱服务,然后获得了授权
    2. 确认有.sh文件和.py文件,注意自己设置路径
    3. 确认crontab设置okay
    4.reboot试试吧!

  • 相关阅读:
    科大讯飞2022秋招笔试知识点总结(超详细)
    【Linux】字节序理解
    电脑案件冲突问题
    java基础 集合2
    createNodeIterator认识
    【自学CSS笔记第9篇】——结构伪类选择器和伪元素
    Part2_扩展MATSIM_Subpart3_个人汽车交通_第14章 电动汽车
    18444 分数拆分
    RestTemplat
    [Qt]多线程和套接字通信
  • 原文地址:https://blog.csdn.net/weixin_43702920/article/details/132717343