树莓的基本环境已经搭建完了,但是有个问题是,如果由于网络变动,导致PI的ip地址发生变化,那么就无法通过ssh进入PI,所以为了能正确获得PI的ip,可以在开机的时候通过PI发送邮件给自己的邮箱,告诉你PI的IP。
1. 写一个python脚本startup_mailer.py,用来发送邮件(参考elinux.org/RPi_Email_IP_On_Boot_Debian)#!/usr/bin/env python import subprocess import smtplib import socket import datetime from email .mime. text import MIMEText from email .mime. multipart import MIMEMultipart from email .mime. image import MIMEImage mailto = 'send email Address' gmail_user = 'Your gmail Address' gmail_password = 'Your gmail password' def send_mail(to_list ,sub): smtpserver = smtplib.SMTP('smtp.gmail.com' ,587) smtpserver .ehlo() smtpserver .starttls() smtpserver .ehlo() smtpserver .login( gmail_user,gmail_password ) today = datetime.date.today arg = 'ip route list' p = subprocess.Popen(arg ,shell= True,stdout =subprocess.PIPE) data = p. communicate() split_data = data[ 0].split () ipaddr = split_datap[split_data.index ('src')+ 1] PI_ip = 'Your ip is %s' % ipaddr content = "<a href = 'http://xdocutju.com'>"+PI_ip +"</a>"+"<br><img src=" +"cid:img1"+">" msgText = MIMEText(content,_subtype ='html', _charset='gb2312' ) fp = open( '/home/pi/ocr_pi.png','rb' ) msgImg = MIMEImage(fp.read ()) fp.close() msgImg .add_header('Content-ID','<img1>' ) msgRoot = MIMEMultipart('multipart') msgRoot = attach(msgText) msgRoot = attach(msgImg) msgRoot ['Subject'] = sub msgRoot ['From'] = gmail_user msgRoot ['To'] = to_list smtpserver .sendmail(gmail_user,[to_list ],msgRoot.as_string()) smtpserver .quit() return True if __name__ == '__main__' if send_main(mailto,'hello from PI' ): print "send success" else print "send fail"2. 将脚本startup_mailer.py移至/etc/init.d/目录下 修改其权限:sudo chmod +x /etc/init.d/startup_mailer.py 代码测试: sudo /etc/init.d/startup_mailer.py start 将脚本加入启动清单,重启,成功 sudo update-rc.d startup_mailer.py defaults 如果以后要删除这个任务,sudo update-rc.d startiup_mailer.py remove Done!