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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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            = [email protected]            # Email to send to.
    mail_user     = [email protected]           # 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] = "[email protected]"
    msg[To] = "[email protected]"
    
    # 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 

 

以上是关于RPi 2B 自动发送获取的IP到固定邮箱的主要内容,如果未能解决你的问题,请参考以下文章

树莓派开机自动发送IP到指定QQ邮箱

CentOS - 开机自动发送IP到指定邮箱 - smtp.163.com

windows ping RPi 2B

python实现树莓派开机自动发送IP到指定邮箱

RPi 2B python opencv camera demo example

树莓派获取ip地址发送到邮箱