码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-02-21 19:58:05      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

/*************************************************************************
 *                 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] = "641283060@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 

 

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

标签:

原文地址:http://www.cnblogs.com/zengjfgit/p/5205338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!