forked from laixintao/Report-IP-hourly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreportip.py
109 lines (96 loc) · 2.85 KB
/
reportip.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python
#-*-coding:utf8-*-
__author__ = 'laixintao'
import socket
import fcntl
import time
import struct
import smtplib
import urllib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import re
import urllib2
# the e-mail config
# this is just a simple format,this e-mail doesn't exist.
smtpserver = "smtp.sina.com"
username = "reaspberrypi@sina.com"
password = "123456"
sender = "reaspberrypi@sina.com"
receiver = ["receiver@sina.com","master@sina.com"]
subject = "[RPI]IP CHANGED"
# file_path config
file_path = "/root/rootcrons/lastip.txt"
def sendEmail(msghtml):
msgRoot = MIMEMultipart('related')
msgRoot["To"] = ','.join(receiver)
msgRoot["From"] = sender
msgRoot['Subject'] = subject
msgText = MIMEText(msghtml,'html','utf-8')
msgRoot.attach(msgText)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
def check_network():
while True:
try:
print "Network is Ready!"
break
except Exception , e:
print e
print "Network is not ready,Sleep 5s...."
time.sleep(10)
return True
def get_lan_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("1.1.1.1",80))
ipaddr=s.getsockname()[0]
s.close()
return ipaddr
class Getmyip:
def getip(self):
try:
myip = self.visit("http://1111.ip138.com/ic.asp")
except:
try:
myip = self.visit("http://ip.chinaz.com/")
except:
try:
myip = self.visit("http://www.whereismyip.com/")
# if you want to add more,use the format "except try"
# make sure the most useful link be the first
except:
print "Fail to get the Network ip."
print "Get the LAN ip."
myip = get_lan_ip()
return myip
def visit(self,url):
opener = urllib2.urlopen(url,timeout=20)
if url == opener.geturl():
str = opener.read()
print "IP information from",url
return re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
def get_network_ip():
getmyip = Getmyip()
localip = getmyip.getip()
return localip
if __name__ == '__main__':
check_network()
ipaddr=get_network_ip()
lanip=get_lan_ip()
emailip=str(ipaddr)+" "+str(lanip)
ip_file = open(file_path)
last_ip = ip_file.read()
ip_file.close()
if last_ip == emailip:
print "IP not change."
else:
print "IP changed."
ip_file = open(file_path,"w")
ip_file.write(str(emailip))
ip_file.close()
sendEmail(ipaddr)
print "Successfully send the e-mail."