RPi 2B 自动发送获取的IP到固定邮箱

简介: /************************************************************************* * RPi 2B 自动发送获取的IP到固定邮箱 * 声明: * 本文主要记录RPi 2B如何自动将IP以邮件的形式发送到邮箱。
/*************************************************************************
 *                 RPi 2B 自动发送获取的IP到固定邮箱
 * 声明:
 *     本文主要记录RPi 2B如何自动将IP以邮件的形式发送到邮箱。
 *
 *                                    2016-2-21 深圳 南山平山村 曾剑锋
 ************************************************************************/

一、参考文档:
    1. RPi Email IP On Boot Debian
        http://elinux.org/RPi_Email_IP_On_Boot_Debian
    2. How to convert list to string [duplicate]
        http://stackoverflow.com/questions/5618878/how-to-convert-list-to-string
    3. Python for Loop Statements
        http://www.tutorialspoint.com/python/python_for_loop.htm

二、cat bootSendEmail.py
    #!/usr/bin/python
    
    import subprocess
    import smtplib
    from email.mime.text import MIMEText
    import datetime
    
    # Change to your own account information
    # Account Information
    to            = '64128306@qq.com'            # Email to send to.
    mail_user     = 'zengjf42@163.com'           # Email to send from.
    mail_password = '填入授权密码'                           # Email password.
    smtpserver    = smtplib.SMTP('smtp.163.com') # Server to use.
    
    smtpserver.ehlo()                            # Says 'hello' to the server
    smtpserver.starttls()                        # Start TLS encryption
    smtpserver.ehlo()
    smtpserver.login(mail_user, mail_password)   # Log in to server
    today = datetime.date.today()                # Get current time/date
    
    arg='ip route list'                          # Linux command to retrieve ip addresses.
    # Runs 'arg' in a 'hidden terminal'.
    p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
    data = p.communicate()                       # Get data from 'p terminal'.
    
    # get ip data
    ip_lines = data[0].splitlines()
    ips = ""
    for ip in ip_lines: 
       ips += ip + "\n"
    
    
    # Creates the text, subject, 'from', and 'to' of the message.
    msg = MIMEText(ips)
    msg['Subject'] = 'IPs For RaspberryPi on %s' % today.strftime('%b %d %Y')
    msg['From'] = "zengjf42@163.com"
    msg['To'] = "64128306@qq.com"
    
    # Sends the message
    smtpserver.sendmail(mail_user, [to], msg.as_string())
    
    # Closes the smtp server.
    smtpserver.quit()

三、 将bootSendEmail.py放入/usr/bin/

四、modify /etc/rc.local
    #!/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"
      bootSendEmail.py                         # add this line
    fi

    exit 0

五、重启系统,并查看邮箱:
    default via 192.168.1.1 dev wlan0 
    default via 192.168.1.1 dev wlan0  metric 303 
    192.168.0.0/24 dev eth0  proto kernel  scope link  src 192.168.0.5 
    192.168.1.0/24 dev wlan0  proto kernel  scope link  src 192.168.1.102 
    192.168.1.0/24 dev wlan0  proto kernel  scope link  src 192.168.1.102  metric 303 

 

目录
相关文章
MindOpt APL 达摩院自己的建模语言!
MindOpt建模语言(MindOpt Algebraic Programming Language, MindOpt APL, 简称为MAPL)是MindOpt团队研发的一种代数建模语言。相比与其他的语言,MAPL语法相对较少且自然,很贴近数学语言。用MAPL描述数学规划模型与用数学公式进行描述非常类似。
MindOpt APL 达摩院自己的建模语言!
|
9月前
|
程序员
HarmonyOS NEXT 实战系列05-案例回关粉丝
本文介绍了一个基于HarmonyOS的组件化设计案例,通过提取 `FansItemComp` 组件实现复用,使用 `@Prop` 动态接收数据渲染UI。示例中包含关注与互关功能:父组件 `TestPage` 提供粉丝列表数据,封装 `getFansAndFollowCount` 方法统计互关人数;子组件通过按钮交互更新关注状态,并利用 `onChange` 回调通知父组件同步数据变化。代码结构清晰,展示了组件间通信及动态渲染的实现方式。
|
分布式计算 DataWorks 大数据
MaxCompute操作报错合集之在开发环境代码运行没问题,生产环境运行报错,是什么导致的
MaxCompute是阿里云提供的大规模离线数据处理服务,用于大数据分析、挖掘和报表生成等场景。在使用MaxCompute进行数据处理时,可能会遇到各种操作报错。以下是一些常见的MaxCompute操作报错及其可能的原因与解决措施的合集。
319 0
|
机器学习/深度学习 自然语言处理 搜索推荐
自然语言处理在智能客服系统中的应用
自然语言处理在智能客服系统中的应用
544 0
|
定位技术
Echarts实战案例代码(27):地理坐标图视觉引导线及富文本提示框的案例
Echarts实战案例代码(27):地理坐标图视觉引导线及富文本提示框的案例
774 0
|
存储 人工智能 NoSQL
MATLAB 之 数值数据,矩阵的表示和变量及其操作
MATLAB 数据类型较为丰富,既有数值型、字符串等基本数据类型,又有结构(Structure)、单元(Cell)等复杂的数据类型。 在 MATLAB 中,没有专门的逻辑型数据,而以数值 1 (非零)表示真,以数值 0 表示假。 MATLAB 各种数据类型都以矩阵形式存在,所以矩阵是 MATLAB 最基本的数据对象形式。
|
弹性计算
阿里云服务器支持巨型帧(Jumbo frames)说明
阿里云服务器支持巨型帧(Jumbo frames)说明,巨型帧(Jumbo frames)是指有效负载超过IEEE 802.3标准所限制的1500字节的以太网帧,增大的有效载荷有助于提高链路利用率,获得更好的网络性能,阿里云服务器部分ECS实例支持
695 0
Qt | 鼠标事件和滚轮事件 QMouseEvent、QWheelEvent
学习使用Qt的鼠标事件和滚轮事件。
1109 0
|
XML 数据库 数据格式
DrugBank:小分子数据信息挖掘
DrugBank:小分子数据信息挖掘
1246 0
DrugBank:小分子数据信息挖掘
datagruad 日常状态检查
datagruad 日常状态检查
364 0