树莓派开机自启动发送ip和wifi信息至邮箱脚本(基于yagmail模块)

背景: 每次运行树莓派时,不知道ip地址和wifi信息,很是麻烦。如果通过脚本来实现自动发送ip和wifi信息到邮箱,那就很方便了。

首先运行前需要安装yagmail库 sudo pip3 install yagmail

使用者只需要修改邮箱号,邮箱账户名和stmp授权码即可

#coding=utf-8
import yagmail
import time
import urllib.request
import subprocess
# 检查网络连通性
def check_network():
    while True:
        try:
            result=urllib.request.urlopen('http://www.baidu.com')
            print (result)
            print ("Network is Ready!")
            break
        except Exception as e:
            print (e)
            print ("Network is not ready,Sleep 5s...")
            time.sleep(5)
    return True
# 运行iwconfig命令行,返回信息
def iwconfig():
    result=subprocess.getoutput('iwconfig') 
    return result
# 运行ifconfig命令行,返回信息
def ifconfig():
    result=subprocess.getoutput('ifconfig') 
    return result
# 发送邮件
def sendEmail():
    check_network()
    yag = yagmail.SMTP(
        user = "xxxxxxxxxx@qq.com",   # 发件人邮箱
        password='************',  # 授权码
        host = 'smtp.qq.com')
    #邮件内容
    contents = [ifconfig(),'#===========分页符===========#', iwconfig()]
    yag.send(to = 'xxxxxxxxxx@qq.com',# 收件人邮箱
             subject = 'Raspberry Pi 4B',  # 邮件主题
             contents = contents)

if  __name__ == '__main__' :
    sendEmail()

stmp授权码是在qq邮箱里面找到,步骤:“设置”>“账户”,下滑找到下图所示。点击“生成授权码”,此时需要验证,验证后将授权码复制即可。

注意:复制粘贴后的码,字母之间没有空格。
比如:“tyeb yyew weew refs”,粘贴后应为“tyebyyewweewrefs”
在“设置”找账户
设置树莓派开机自启动其代码。在这里根目录下创建autoIP.py文件,当然你可以自己拟定名字。
在这里插入图片描述
使用sudo vim /etc/rc.local命令,修改代码如图所示,保存即可。(注意你的路径要正确)。
我这里使用的是vim,你也可以使用nano,个人习惯吧,vim就不介绍怎么使用了。

在这里插入图片描述
接着重启系统就可以了,效果如下。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

几何木偶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值