forked from Geek-Roc/auto-public-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_ip.py
109 lines (95 loc) · 3.14 KB
/
auto_ip.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/local/bin/python3
# coding=utf-8
import smtplib
from email.mime.text import MIMEText
from urllib import request
from collections import Counter
import re
import time
import threading
textList = []
sendIPAddress = ''
def sendIP(content):
# config
host = 'smtp.***.com'
port = 465
sender = '***@***.***'
receiver = ['***@***.***',
'***@***.***',
'***@***.***']
pwd = '***' # Password
# core
msg = MIMEText(content, _subtype='plain', _charset='utf-8')
msg['subject'] = 'IP changed. Please handle it quickly'
msg['from'] = sender
msg['to'] = ",".join(receiver)
try:
hero = smtplib.SMTP_SSL(host=host, port=port)
hero.login(sender, pwd)
hero.sendmail(sender, receiver, msg.as_string())
except Exception as e:
print('error:', str(e))
else:
print('IP is sended successly.')
def getIP():
urls = ['https://jsonip.com/',
'https://www.ip.cn/',
'https://www.ipip.net/',
'http://ip111.cn/']
#Simulate browser access to avoid being blocked by the site
header = {
'Connection': 'Keep-Alive',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36'
}
ips = []
for url in urls:
try:
req = request.Request(url, headers=header)
html = request.urlopen(req).read()
print('request:' + url)
content = html.decode('utf-8') # utf-8 decode
pattern = re.compile(
r'((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))', re.M | re.I)
ipAddress = pattern.search(content).group(0)
except Exception as e:
textList.append('error:' + str(e))
print('error:' + str(e))
else:
textList.append(url)
textList.append(ipAddress)
ips.append(ipAddress)
print('success:' + url + ":" + ipAddress)
ipsCounter = Counter(ips)
if len(ipsCounter) > 0:
ip = ipsCounter.most_common()[0][0]
return ip
def checkIP():
global sendIPAddress
print('Checking...')
nowIp = getIP()
if sendIPAddress != nowIp:
sendIPAddress = nowIp
print('IP changed '+nowIp)
textList.insert(0, sendIPAddress)
nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
textList.insert(1, nowtime)
content = '\n'.join(textList)
sendIP(content)
else:
print('IP not change')
textList.clear()
t = threading.Timer(66.0, checkIP)
t.start()
if __name__ == "__main__":
print("start")
sendIPAddress = getIP()
textList.insert(0, sendIPAddress)
nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
textList.insert(1, nowtime)
content = '\n'.join(textList)
sendIP(content)
textList.clear()
t = threading.Timer(66.0, checkIP)
t.start()