RPi 2B 自动发送获取的IP到固定邮箱
2016-02-21 18:28
579 查看
/************************************************************************* * 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
相关文章推荐
- 二级指针
- HDU 2602 Bone Collector(01背包)
- 亿级Web系统搭建——单机到分布式集群
- 019 virtualbox虚拟机无法上网的一种情况
- PHP MySQL Order By 关键词 之 Order By
- 自己动手丰衣足食之下拉列表
- java容器--ArrayList源码简析
- 安装spf13之后,设置nospell和colorcolumns
- mysql Access denied for user 'root'@'localhost' (using password: YES)
- 用android studio创建第一个安卓程序加载html5页面(一)
- 解决vs2010头文件stdatx.h预编译问题
- 硬中断和软中断
- Educational Codeforces Round 8 E. Zbazi in Zeydabad 树状数组
- 数组与字符串
- XML解析
- Leetcode 292
- java 之 集合遍历,存储结构,集合特点
- javaScript中自定义滚动条一
- 爬虫简单架构
- PHP MySQL Where 子句 之Where