-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmac_routes.py
71 lines (67 loc) · 1.7 KB
/
mac_routes.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
#!/usr/bin/env python
#-*-coding:utf-8-*-
import os,sys,urllib,commands
def dowl_file():
'''
下载最新的ip列表
'''
os.remove('delegated-apnic-latest')
url = 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'
print 'download'
urllib.urlretrieve(url,'delegated-apnic-latest')
def cn_list():
'''
根据列表文件生成中国ip列表
'''
latest_ip = file('delegated-apnic-latest','rb')
cn_ip = file('cn_ip','wb')
for lines in latest_ip.xreadlines():
if lines.find('apnic|CN|ipv4') != -1:
line = lines.split('|')
if line[1] == 'CN' and line[2] == 'ipv4':
mask = 32
while line[4] > 1 :
line[4] = int(line[4])/2
mask-=1
w_data = '%s/%s\n' %(line[3],str(mask))
cn_ip.write(w_data)
def def_gw():
'''
en0 网卡的默认网关
'''
def_gw = commands.getoutput("netstat -nr |grep default|grep en0|awk '{print $2}'")
return def_gw
def route_change(act,gw):
'''
修改路由表
'''
cn_ip_r = file('cn_ip','rb')
for line in cn_ip_r.xreadlines():
ip = line.strip('\n')
#test =
commands.getoutput("sudo route %s %s %s &>/dev/null" %(act,ip,gw))
#print test
#test
def start(args):
if args == 'new':
dowl_file()
cn_list()
route_change('add',def_gw())
elif args == 'add':
route_change('add',def_gw())
elif args == 'change':
route_change('change',def_gw())
elif args == 'del':
route_change('delete',def_gw())
elif args == 'reset':
dowl_file()
route_change('delete',def_gw())
cn_list()
route_change('add',def_gw())
else:
help_test = '''
new | add | change | del | reset
'''
print help_test
if __name__ == "__main__":
start(sys.argv[1])