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 

 

目录
打赏
0
0
0
0
12
分享
相关文章
VOS 媒体转发开启后,如何计算各种编码所占用的带宽,以及如何修改服务器的时区
计算方法如下: 带宽 = 包长度 × 每秒包数 = 包长度 × (1 / 打包周期) =(Ethernet 头 + IP 头 + UDP 头 + RTP 头 + 有效载荷)× 每秒包数 =(112bit + 160bit + 64bit + 96bit + 有效载荷)× 每秒包数 =(112bit + 320bit + 有效载荷)× 每秒包数 =(432bit + 有效载荷)× 每秒包数 =(432bit × 每秒包数) + (有效载荷 × 每秒包数) =(432bit × 1000 / 打包周期) + 编码速率 =(432bit / 打包周期)Kbps + 编码速率 按照上面的计算公式: G
树莓派2B设置固定ip
​​不要修改/etc/network/interfaces文件 在/etc/dhcpcd.conf文件后面增加 interface eth0static ip_address=192.
1575 0
Linux网络环境配置:(内含:随机ip和固定ip设置方式)
Linux网络环境配置:(内含:随机ip和固定ip设置方式)
315 0
Linux网络环境配置:(内含:随机ip和固定ip设置方式)
Linux虚拟机设置固定IP
Linux虚拟机设置固定IP
199 2
ubuntu 15.10 设置静态ip 分配固定ip 设置dns 设置网关 命令行配置ip 固定ip不生效怎么办
要用到的文件: 配置接口信息 /etc/network/interfaces 配置内容: auto eth0 iface eth0 inet static address 192.168.216.
1381 0
C# 设置IP地址及设置自动获取IP
原文:C# 设置IP地址及设置自动获取IP 1.添加引用"system.Management" 2.添加using System.Management using System; using System.
2781 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等