TA的每日心情 | 擦汗 2015-3-20 17:00 |
---|
签到天数: 13 天 连续签到: 1 天 [LV.3]偶尔看看II
|
因为树莓派放在家里用,家里的电信宽带ip老是变动,就用了这两个方法。
1.ip变化时发邮件通知
config.py 配置邮箱信息- #!/usr/bin/env python
- #-*-coding:utf-8-*-
- smtpServer='smtp.126.com' #邮件发送帐户的smtp服务器地址
- smtpPort='25' #邮件发送帐户的smtp服务器发送端口
- smtpUser='xxxxx@126.com' #邮件发送帐户名
- smtpPwd='xxxx' #邮件发送帐户密码,我这里打X号示例,但老兄得填真的
- sendTo='xxxx@163.com' #接收邮箱地址
复制代码- #!/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()
复制代码 SendIP.py脚本负责检查发送。
用 nohup python ./SendIP.py & 后台启动服务就好啦
第二种:原先在家里搭建了个服务,要更改域名地址,用花生壳又不太方便,就用dnspod提供的api接口写个py脚本,用于动态更新域名地址,不过生效不是很快。也发出来给大家看下。- 1 #!/usr/bin/env python
- 2 #-*-coding:utf-8-*-
- 3 import httplib,urllib
- 4 import json
- 5 import re,urllib2
- 6 import time
- 7
- 8 class Getmyip:
- 9 def getip(self):
- 10 try:
- 11 myip = self.visit("http://www.whereismyip.com/")
- 12 # return myip
- 13 except:
- 14 try:
- 15 myip = self.visit("http://www.bliao.com/ip.phtml")
- 16 # return myip
- 17 except:
- 18 try:
- 19 myip = self.visit("http://www.whereismyip.com/")
- 20 # return myip
- 21 except:
- 22 myip = "So sorry!!!"
- 23 return myip
- 24
- 25 def visit(self,url):
- 26 opener = urllib2.urlopen(url)
- 27 if url == opener.geturl():
- 28 str = opener.read()
- 29 asd=re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
- 30 return asd
- 31
- 32 def setdnspod(newip):
- 34 params = 'login_email=xxxxx&login_password=xxxxx&format=json&domain_id=16415545&record_id=67237927&record_line=默认&sub_domain=www&value='+ newip +'&record_type=A'
- 35 header = {"Content-type": "application/x-www-form-urlencoded",
- 36 "Accept": "text/plain"}
- 37 conn = httplib.HTTPSConnection("dnsapi.cn")
- 38 #获取api版本信息
- 39 #conn.request("POST","/Info.Version",params,header)
- 40 #获取domains列表
- 41 #已知"id": xxxx,"name": "xxxx"
- 42 #conn.request("POST","/Domain.List",params,header)
- 43 #设置ddns
- 44 conn.request("POST","/Record.Modify",params,header)
- 45 #获取记录列表
- 46 #已知 "id": "xxxx", "name": "blog", "id": "xxxx","name": "www",
- 47 #conn.request("POST","/Record.List",params,header)
- 48 response = conn.getresponse()
- 49 #print response.status, response.reason
- 50 data = response.read()
- 51 #print(data)
- 52 conn.close()
- 53 s = json.loads(data)
- 54 return s["status"]["code"],data
- 55
- 56 if __name__ == '__main__':
- 57 getmyip = Getmyip()
- 58 localip=0
- 59 while(1):
- 60 time.sleep(1)
- 61 tm_str_start = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
- 62 print(tm_str_start+'\n')
- 63 localtmp = getmyip.getip()
- 64 if (localtmp == None):
- 65 print('GET LOCAL IP ERROR!\n')
- 66 else:
- 67 if (localip != localtmp):
- 68 localip = localtmp
- 69 code,data = setdnspod(localip)
- 70 if (code == '1'):
- 71 print('set new ip success:' + localip +'\n')
- 72 else:
- 73 print('set new ip failed:' + code + ':' + data + '\n')
- 74 else:
- 75 print('new ip == old ip'+':'+localip+':'+localtmp+'\n')
复制代码 |
|