-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetwifi-haizhi.py
229 lines (213 loc) · 7.81 KB
/
getwifi-haizhi.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#codingutf-8
import os
import re
import sys
import time
from selenium import webdriver
import requests
wiredipaddr=''
wirelessipaddr=''
def setLanRoute(str):
r = os.popen(str)
info = r.readlines()
print("len", len(info))
print(info)
while (len(info) != 1):
r = os.popen(str)
time.sleep(1)
info = r.readlines()
print(info)
# 设置route函数,输入为内网有线ip
def setLANroute(ipaddr):
print("setLANroute for " + ipaddr)
os.popen('route delete 0.0.0.0')
os.popen('route delete 0.0.0.0')
os.popen('route delete 10.0.0.0')
os.popen('route delete 192.168.1.1')
os.popen('route delete 192.168.1.0')
os.popen('route delete 224.0.0.0')
# os.popen('route add 192.168.1.1 mask 255.255.255.255 '+ ipaddr + ' if '+ '12')
os.popen('route add 192.168.1.1 mask 255.255.255.255 '+ ipaddr + ' if '+ '12')
os.popen('ipconfig /flushdns')
# os.popen('route add 224.0.0.0 mask 240.0.0.0 '+ ipaddr + ' if '+ '12')
# os.popen('route add 10.0.0.0 mask 255.0.0.0 '+ ipaddr + ' if '+ '12')
# os.popen('route add 192.168.1.0 mask 255.255.255.0 '+ ipaddr + ' if '+ '12')
# setLanRoute('route add 192.168.1.0 mask 255.255.255.0 '+ ipaddr + ' if '+ '12')
# setLanRoute('route add 224.0.0.0 mask 240.0.0.0 192.168.1.1 if '+ '12')
# setLanRoute('route add 10.0.0.0 mask 255.0.0.0 192.168.1.1 if '+ '12')
if ipaddr.find("10.75.") != -1:
print("Add route for 10.75")
setLanRoute('route add 224.0.0.0 mask 240.0.0.0 10.75.10.1 if '+ '12')
setLanRoute('route add 10.0.0.0 mask 255.0.0.0 10.75.10.1 if '+ '12')
print("Add route for 10.75 end")
else:
print("Add route for 192.168")
setLanRoute('route add 224.0.0.0 mask 240.0.0.0 192.168.1.1 if '+ '12')
setLanRoute('route add 10.0.0.0 mask 255.0.0.0 192.168.1.1 if '+ '12')
setLanRoute('route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 if '+ '12')
print("Add route for 192.168 end")
r = os.popen('route add 0.0.0.0 mask 0.0.0.0 '+ ipaddr + ' if '+ '12')
info = r.readlines()
print("len", len(info))
print(info)
while (len(info) != 1):
r = os.popen('route add 0.0.0.0 mask 0.0.0.0 '+ ipaddr + ' if '+ '12')
info = r.readlines()
print("len", len(info))
print(info)
time.sleep(1)
# 设置route函数,输入为外网无线ip
def setWLANroute(ipaddr):
print("setWLANroute for " + ipaddr)
os.popen('route delete 1.1.1.1')
os.popen('route delete 8.8.8.8 ')
os.popen('route delete 4.2.2.2')
r = os.popen('route change 0.0.0.0 mask 0.0.0.0 '+ ipaddr )
info = r.readlines()
print("len", len(info))
print(info)
while (len(info) != 1):
r = os.popen('route change 0.0.0.0 mask 0.0.0.0 '+ ipaddr )
time.sleep(1)
info = r.readlines()
print(info)
os.popen('route add 1.1.1.1 mask 255.255.255.255 '+ ipaddr)
os.popen('route add 8.8.8.8 mask 255.255.255.255 '+ ipaddr)
os.popen('route add 4.2.2.2 mask 255.255.255.255 '+ ipaddr)
# 查找ip
wlanipaddr = '0'
def getipaddr():
a_config=os.popen('ipconfig')
s_config=a_config.read()
c_config=re.findall(' IPv4 Address. . . . . . . . . . . :(.*?)\n',s_config,re.S)
print(c_config)
lanipaddr = '0'
global wlanipaddr
wlanipaddr = '0'
for i in c_config:
if i.find("192.168.1.") != -1:
print("lanipaddr "+i)
lanipaddr = i
break
if i.find("10.75.") != -1:
print("lanipaddr "+i)
lanipaddr = i
break
for i in c_config:
if i.find("172.31.") != -1 :
print("wlanipaddr ip allocated: " + i )
wlanipaddr = i
elif i.find("172.26.") != -1 :
print("wlanipaddr ip allocated: " + i )
wlanipaddr = i
return lanipaddr, wlanipaddr
# 连接
def connectWIFI():
os.popen('netsh wlan disconnect')
print('connect AP ....')
os.popen('netsh wlan connect AP')
time.sleep(3)
print('get IPaddress')
global wiredipaddr
global wirelessipaddr
wiredipaddr, wirelessipaddr = getipaddr()
count = 0
while (count < 10):
if wirelessipaddr.find("172.") != -1 and (wiredipaddr.find("10.75.") or wiredipaddr.find("192.168.1.")):
print("set route")
setLANroute(wiredipaddr)
setWLANroute(wirelessipaddr)
break
else:
time.sleep(2)
count = count + 1
print('Waiting IPaddress ...', count)
wiredipaddr, wirelessipaddr = getipaddr()
if(count == 10):
print("ip not allocated !!! exit!!")
sys.exit()
"""
print('check IPaddress')
if wirelessipaddr.find("172.31.") != -1 and wiredipaddr.find("10.75."):
print("set route")
setLANroute(wiredipaddr)
setWLANroute(wirelessipaddr)
else:
print("ip not allocated, wait 5 seconds")
time.sleep(10)
print('get IPaddress again')
wiredipaddr, wirelessipaddr = getipaddr()
print('check IPaddress again')
if wirelessipaddr.find("172.31.") != -1 and wiredipaddr.find("10.75."):
print("set route again")
setLANroute(wiredipaddr)
setWLANroute(wirelessipaddr)
else:
print("ip not allocated again")
exit(-1)
"""
# 输入验证
def enterPassword():
browser = webdriver.Chrome()
#browser = webdriver.HtmlUnitDriver()
r = browser.get('https://1.1.1.1/login.html') #像目标url地址发送get请求,返回一个response对象
#print(browser.page_source)
input_username = browser.find_element_by_name("username")
input_username.clear() #清空搜索框中的内容
input_username.send_keys("user") #在搜索框中输入**user**
input_password = browser.find_element_by_name("password")
input_password.clear() #清空搜索框中的内容
input_password.send_keys("pass") #在搜索框中输入**pass**
button_submit = browser.find_element_by_name("Submit")
button_submit.click() #相当于回车键,提交
#browser.close()
browser.quit()
def reroute(len_0):
print("rerouting....")
os.popen('route delete 0.0.0.0')
while(len_0 !=0 ):
route_r = os.popen('route print 0.0.0.0 ')
route_str=route_r.read()
route_c=re.findall('0.0.0.0 0.0.0.0(.*)',route_str)
len_0 = len(route_c)
print("wait for zero....")
time.sleep(1)
global wirelessipaddr
r = os.popen('route add 0.0.0.0 mask 0.0.0.0 '+ wirelessipaddr )
info = r.readlines()
print("len", len(info))
print(info)
while (len(info) != 1):
r = os.popen('route add 0.0.0.0 mask 0.0.0.0 '+ wirelessipaddr )
info = r.readlines()
print("len", len(info))
print(info)
time.sleep(1)
def main_loop():
global wlanipaddr
while True:
print(time.strftime('%Y-%m-%d %H:%M:%S----',time.localtime(time.time()))+wlanipaddr)
time.sleep(30)
exit_code = os.system('ping -n 1 -w 1000 www.baidu.com')
if exit_code:
route_r = os.popen('route print 0.0.0.0 ')
route_str=route_r.read()
route_c=re.findall('0.0.0.0 0.0.0.0(.*)',route_str)
len_0 = len(route_c)
if len_0 > 1 :
reroute(len_0)
continue
print("Disconnected, reconnect....")
try:
connectWIFI()
enterPassword()
except Exception as err:
print("Something went wrong!")
print(err)
finally:
print("Next loop!")
connectWIFI()
enterPassword()
main_loop()
print("test run ok")
sys.exit()