自动更换ip上网

我的网络比较坑,每天一个ip限制了特定流量,如果超过限制就需要换个ip才能上网。所以写了个脚本自动更换ip上网。
环境为windows系统。

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
import os
import time
import wmi
def (ip):
wmiService = wmi.WMI()
colNicConfigs = wmiService.Win32_NetworkAdapterConfiguration(IPEnabled = True)
if len(colNicConfigs) < 1:
print 'ûÓÐÕÒµ½¿ÉÓõÄÍøÂçÊÊÅäÆ÷'
return 0
objNicConfig = colNicConfigs[0]
#print objNicConfig
#for method_name in objNicConfig.methods:
# method = getattr(objNicConfig, method_name)
# print method
arrIPAddresses = [ip]
arrSubnetMasks = ['255.255.255.0']
intReboot = 0
returnValue = objNicConfig.EnableStatic(IPAddress = arrIPAddresses, SubnetMask = arrSubnetMasks)
if returnValue[0] == 0:
#print 'ÉèÖÃIP³É¹¦'
pass
elif returnValue[0] == 1:
#print 'ÉèÖÃIP³É¹¦'
intReboot += 1
else:
print 'ÐÞ¸ÄIPʧ°Ü: IPÉèÖ÷¢Éú´íÎó'
return 0
if intReboot > 0:
print 'ÐèÒªÖØÐÂÆô¶¯¼ÆËã»ú'
#print 'ÐÞ¸ÄIP½áÊø'
return 1
import urllib2
def check_connect():
try:
urllib2.urlopen('https://www.baidu.com/', timeout=10)
return 1
except:
return 0
if __name__ == '__main__':
while True:
if check_connect():
print 'network is ok'
while True:
time.sleep(60)
if not check_connect():
break
for i in range(2, 254):
ip = '10.104.171.'+str(i)
print 'try ip:' + ip
if config_ip(ip):
time.sleep(5)
if not check_connect():
pass
else:
print 'current ip is:' + ip
while True:
time.sleep(60)
if not check_connect():
break
else:
print 'config ip error'

代码