-
Notifications
You must be signed in to change notification settings - Fork 0
/
tellMyIp.py
43 lines (37 loc) · 1.23 KB
/
tellMyIp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#coding=utf8
import os
import subprocess
import sys
import time
from weibo import send_weibo
from weibo import getWeiboByID
import setting
def getOldIP():
oldIP = ''
if not os.path.exists('/home/pczb/miui_bbs/weibo_id.ini'):
return oldIP
with open("/home/pczb/miui_bbs/weibo_id.ini") as ifd:
weiboid = ifd.readline().rstrip()
if weiboid != '':
weibo = getWeiboByID(weiboid, setting.oauthKey)
if weibo != None and 'id' in weibo:
oldIP = weibo["text"]
return oldIP
def updateIP():
newIP = getDeviceIP('eth1')
oldIP = getOldIP()
if newIP != oldIP:
print newIP, '###',oldIP
weiboid = send_weibo(newIP, setting.oauthKey)
if weiboid != None and weiboid != '':
outfile = open("/home/pczb/miui_bbs/weibo_id.ini","w")
outfile.write(weiboid)
outfile.close()
def getDeviceIP(interface):
subp = subprocess.Popen("ifconfig "+interface,stdout = subprocess.PIPE,shell = True)
outmsg = subp.communicate()[0]
ipaddr = outmsg.split('\n')[1].split()[1]
last_ipaddr = ipaddr.split('.')[3]
return last_ipaddr
if __name__ == "__main__":
updateIP()