动态获取树莓派的变动外网ip

ip变化时发邮件通知

在此目录下创建 /home/config.py 配置邮箱信息

#!/usr/bin/env python
#-*-coding:utf-8-*-
smtpServer='smtp.126.com' #邮件发送帐户的smtp服务器地址
smtpPort='25' #邮件发送帐户的smtp服务器发送端口
smtpUser='[email protected]' #邮件发送帐户名
smtpPwd='xxxx' #邮件发送帐户密码,我这里打X号示例,但老兄得填真的
sendTo='[email protected]' #接收邮箱地址

在此目录下创建 /home/sendIP.py脚本 负责检查发送。
然后在/etc/rc.local里加上下面这行
/usr/bin/python /home/sendIP.py 启动时运行
代码如下

#!/usr/bin/env python
#-*-coding:utf-8-*-
import smtplib,config,email,sys,socket,threading
from email.Message import Message
import re,urllib2

class Getmyip:
        def getip(self):
                try:
                        myip = self.visit("http://www.whereismyip.com/")
#                   return myip
                except:
                        try:
                                myip = self.visit("http://www.bliao.com/ip.phtml")
#                           return myip
                        except:
                                try:
                                        myip = self.visit("http://www.whereismyip.com/")
#                                   return myip
                                except:
                                        myip = "So sorry!!!"
                return myip

        def visit(self,url):
                opener = urllib2.urlopen(url)
                if url == opener.geturl():
                        str = opener.read()
#                        print str
                        asd=re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
                        return asd

def connect():
        try:
                server=smtplib.SMTP(config.smtpServer,config.smtpPort)
                server.ehlo()
                server.login(config.smtpUser,config.smtpPwd)
                return server
        except Exception:
                print 'connect smtp server failed!'

def sendmessage(server,to,subj,content):
        msg = Message()
        msg['Mime-Version']='1.0'
        msg['From']        = config.smtpUser
        msg['To']          = to
        msg['Subject'] = subj
        msg.set_payload(content)
        try:        
                failed = server.sendmail(config.smtpUser,to,str(msg))
        except Exception ,ex:
                print Exception,ex
                print 'Error - send failed'
        else:
                print 'send succeed!'

#每隔10s检查一次IP,间隔时间可以适当调大一点
def checkIP():
        global text
        #getmyip = Getmyip()
        print text
        ipnow=getmyip.getip()
        print ipnow
        if  text!=ipnow:
                text=ipnow
                server=connect()
                sendmessage(server,to,subj,text)
        t = threading.Timer(600.0, checkIP)
        t.start()

if __name__=="__main__":
        getmyip = Getmyip()
        to=config.sendTo
        subj='ip changed' 
        text = getmyip.getip() #这个就是获取本机IP的
        server=connect()
        sendmessage(server,to,subj,text)
        t = threading.Timer(600.0, checkIP)
        t.start()

你可能感兴趣的:(raspberry,pi,python,树莓派,raspberryP,外网ip)