-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
145 lines (112 loc) · 3.85 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from requests import get,post
from html.parser import HTMLParser
import re
import os
baseUrl = 'https://www.ipaddress.com/ip-lookup'
githubLinks={
'github.global.ssl.fastly.net': '',
# 'github.com': '',
'github.githubassets.com': '',
'collector.githubapp.com': '',
'avatars.githubusercontent.com': ''
}
class GetIp(HTMLParser):
'''解析html文档获取ip'''
def __init__(self):
HTMLParser.__init__(self)
self.text = None
self.flag = 'start'
self.ips = []
def handle_starttag(self, tag, attrs):
'''标签开始'''
if tag == 'h1' and self.flag == 'start':
self.flag = "pending"
if tag == 'input' and attrs.__contains__(('name', "host")) and attrs.__contains__(("type", "radio")):
for (k, v) in attrs:
if k == 'value':
self.ips.append(v)
def handle_data(self, data):
'''读取内容容'''
if self.flag == 'pending' and self.text == None:
# print("data==========>", data)
self.flag = 'end'
regText = re.search(r'\d+\.\d+\.\d+\.\d+', data)
if regText != None:
self.text = regText.group()
def print_data(self):
''' 输出ip '''
# print("print", self.text, self.ips)
if self.text != None:
# print("text",self.text)
return self.text
else:
# print("ips",self.ips)
return self.ips[0]
def reset(self):
''' 重置HTMLParser实例 '''
self.text = None
self.flag = 'start'
self.ips = []
HTMLParser.reset(self)
class writeHostsFile():
''' 操作hosts文件 '''
def __init__(self):
self.file_read = None
self.path = self.getHostFilePath()
self.content = []
def getHostFilePath(self):
''' 获取hosts文件路径'''
winDir = [os.getenv("windir"), "System32", "drivers", "etc", "hosts"]
return "\\".join(winDir)
def open(self):
''' 打开hosts文件'''
self.file_read = open(self.path, mode='r+')
self.content = self.file_read.readlines()
def replice(self, str_text, r_str):
'''替换hosts内容'''
for index, item in enumerate(self.content):
if item.startswith("#") == False and item.find(str_text) != -1:
print("原始字符串", item)
print("替换字符串", r_str)
self.content[index] = r_str + '\n'
return
print("新增:%s" % (r_str))
self.content.append(r_str + '\n')
def resriteFile(self):
self.file_read.seek(0, 0)
self.file_read.truncate(0)
self.file_read.writelines(self.content)
def end(self):
self.resriteFile()
self.file_read.close()
# print(self.content)
def flush_dns():
os.system("ipconfig /flushdns")
def sendResquest(url):
data={ "host": url }
baseUrl = 'https://www.ipaddress.com/ip-lookup'
headers={
"Accept": "*/*",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
}
return post(baseUrl, data=data, headers=headers).text
def sendAll(data):
host_file = writeHostsFile()
host_file.open()
print("<======== 开始 =========>")
for key in data.keys():
print("处理域名:%s" % (key))
parser = GetIp()
resText = sendResquest(key)
parser.feed(resText)
ip = parser.print_data()
parser.reset()
r_str = "%s %s" % (ip, key)
host_file.replice(key, r_str)
print("<======== 写入 =========>")
host_file.end()
print("<======== 刷新dns =========>")
flush_dns()
sendAll(githubLinks)
input("按任意键关闭")