forked from Shinlor/WinRoute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinroute.py
308 lines (241 loc) · 11.5 KB
/
winroute.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/python
# -*- coding:utf-8 -*-
#
__author__ = 'Shinlor'
import ctypes
import socket
import struct
DWORD = ctypes.c_ulong
NO_ERROR = 0
NULL = ""
bOrder = 0
ANY_SIZE=1
class MIB_IPFORWARDROW(ctypes.Structure):
_fields_ = [('dwForwardDest', DWORD),#目标IP
('dwForwardMask', DWORD),#网络掩码
('dwForwardPolicy', DWORD),#多径路由策略
('dwForwardNextHop', DWORD),#下一跳IP
('dwForwardIfIndex', DWORD),#接口索引
('dwForwardType', DWORD),#路由类型
('dwForwardProto', DWORD),#路由协议
('dwForwardAge', DWORD),#路由时间
('dwForwardNextHopAS', DWORD),#下一跳编号
('dwForwardMetric1', DWORD),#跃点数
('dwForwardMetric2', DWORD),
('dwForwardMetric3', DWORD),
('dwForwardMetric4', DWORD),
('dwForwardMetric5', DWORD)]
class WinRoute():
def __init__(self):
self.DWORD = ctypes.c_ulong
self.NULL = ""
self.NO_ERROR = 0
self.bOrder = 0
self.ANY_SIZE=1
#得到默认路由的参数
IpForwardTablelist=self.getroute()#获得当前路由表
for tmp in IpForwardTablelist:
if tmp["ForwardDest"]=="0.0.0.0": #查找默认路由的有关参数,除要修改的部分,其他元素使用默认路由的值
#如果不给定网关,则默认为默认路由的网关
self.dwForwardNextHop = struct.unpack("I",socket.inet_aton(tmp["ForwardNextHop"]))[0]
self.dwForwardPolicy = struct.unpack("I",socket.inet_aton(tmp["ForwardPolicy"]))[0]
#如果不给定ForwardIfIndex,则默认为默认路由的接口
self.dwForwardIfIndex=tmp["ForwardIfIndex"]
self.dwForwardType=tmp["ForwardType"]
self.dwForwardProto=tmp["ForwardProto"]
self.dwForwardAge=tmp["ForwardAge"]
self.dwForwardNextHopAS=tmp["ForwardNextHopAS"]
#win7及win10无法设定小于默认路由的Metric值,这里使用了相加。若不给定,最终与默认路由的Metric相同
self.dwForwardMetric1=tmp["ForwardMetric1"]
self.dwForwardMetric2=tmp["ForwardMetric2"]
self.dwForwardMetric3=tmp["ForwardMetric3"]
self.dwForwardMetric4=tmp["ForwardMetric4"]
self.dwForwardMetric5=tmp["ForwardMetric5"]
pass
def printroute(self):
#得到路由表的列表
IpForwardTablelist=self.getroute()
#格式化输出
print( "%-13s %-11s %-12s %-12s %-4s %-3s %-4s %-3s %-4s %-3s %-3s %-3s %-3s %-3s" %
(
'目标IP',
'网络掩码',
'多径路由策略',
'下一跳IP',
'接口索引',
'路由类型',
'路由协议',
'路由时间',
'下一跳编号',
'Metric1',
'Metric2',
'Metric3',
'Metric4',
'Metric5')
)
for tmp in IpForwardTablelist:
print("%-13s %-11s %-12s %-12s %-4s %-3s %-4s %-3s %-4s %-3s %-3s %-3s %-3s %-3s" %
(tmp['ForwardDest'],
tmp['ForwardMask'],
tmp['ForwardPolicy'],
tmp['ForwardNextHop'],
tmp['ForwardIfIndex'],
tmp['ForwardType'],
tmp['ForwardProto'],
tmp['ForwardAge'],
tmp['ForwardNextHopAS'],
tmp['ForwardMetric1'],
tmp['ForwardMetric2'],
tmp['ForwardMetric3'],
tmp['ForwardMetric4'],
tmp['ForwardMetric5'],
)
)
def getroute (self):
DWORD=self.DWORD
NULL=self.NULL
NO_ERROR=self.NO_ERROR
bOrder=self.bOrder
ANY_SIZE=self.ANY_SIZE
dwSize = DWORD(0)
IpForwardTablelist=[]
# call once to get dwSize
dwStatus=ctypes.windll.iphlpapi.GetIpForwardTable(NULL, ctypes.byref(dwSize), bOrder)
#print (dwStatus)
# ANY_SIZE is used out of convention (to be like MS docs); even setting this
# to dwSize will likely be much larger than actually necessary but much
# more efficient that just declaring ANY_SIZE = 65500.
# (in C we would use malloc to allocate memory for the *table pointer and
# then have ANY_SIZE set to 1 in the structure definition)
ANY_SIZE = dwSize.value
class MIB_IPFORWARDTABLE(ctypes.Structure):
_fields_ = [('dwNumEntries', DWORD), ('table', MIB_IPFORWARDROW * ANY_SIZE)]
#print (ANY_SIZE)
IpForwardTable=MIB_IPFORWARDTABLE()
if (ctypes.windll.iphlpapi.GetIpForwardTable(ctypes.byref(IpForwardTable),
ctypes.byref(dwSize), bOrder) == NO_ERROR):
maxNum = IpForwardTable.dwNumEntries
#print(IpForwardTable.dwNumEntries)
#print (IpForwardTable.table)
placeHolder = 0
# loop through every connection
while placeHolder < maxNum:
item = IpForwardTable.table[placeHolder]
IpForwardDcit={}
placeHolder += 1
ForwardNextHop=item.dwForwardNextHop
ForwardDest=item.dwForwardDest
ForwardDest=socket.inet_ntoa(struct.pack('L', ForwardDest))
ForwardMask=item.dwForwardMask
ForwardMask=socket.inet_ntoa(struct.pack('L', ForwardMask))
ForwardPolicy=item.dwForwardPolicy
ForwardPolicy=socket.inet_ntoa(struct.pack('L', ForwardPolicy))
ForwardNextHop=item.dwForwardNextHop
#使用socket模块,转换32位打包的IPV4地址为IP地址的标准点号分隔字符串表示
ForwardNextHop = socket.inet_ntoa(struct.pack('L', ForwardNextHop))
ForwardIfIndex=item.dwForwardIfIndex
ForwardType=item.dwForwardType
ForwardProto=item.dwForwardProto
ForwardAge=item.dwForwardAge
ForwardNextHopAS=item.dwForwardNextHopAS
ForwardMetric1=item.dwForwardMetric1
ForwardMetric2=item.dwForwardMetric2
ForwardMetric3=item.dwForwardMetric3
ForwardMetric4=item.dwForwardMetric4
ForwardMetric5=item.dwForwardMetric5
#字典IpForwardDcit赋值
IpForwardDcit["ForwardDest"]=ForwardDest
IpForwardDcit["ForwardMask"]=ForwardMask
IpForwardDcit["ForwardPolicy"]=ForwardPolicy
IpForwardDcit["ForwardNextHop"]=ForwardNextHop
IpForwardDcit["ForwardIfIndex"]=ForwardIfIndex
IpForwardDcit["ForwardType"]=ForwardType
IpForwardDcit["ForwardProto"]=ForwardProto
IpForwardDcit["ForwardAge"]=ForwardAge
IpForwardDcit["ForwardNextHopAS"]=ForwardNextHopAS
IpForwardDcit["ForwardMetric1"]=ForwardMetric1
IpForwardDcit["ForwardMetric2"]=ForwardMetric2
IpForwardDcit["ForwardMetric3"]=ForwardMetric3
IpForwardDcit["ForwardMetric4"]=ForwardMetric4
IpForwardDcit["ForwardMetric5"]=ForwardMetric5
IpForwardTablelist.append(IpForwardDcit)
return IpForwardTablelist
def CreateIpForwardEntry(self,dwForwardDest,dwForwardMask,
dwForwardNextHop=None,dwForwardMetric=0,dwForwardIfIndex=None):
DWORD=self.DWORD
NULL=self.NULL
NO_ERROR=self.NO_ERROR
bOrder=self.bOrder
ANY_SIZE=self.ANY_SIZE
pRoute=MIB_IPFORWARDROW() #建立结构域
pRoute.dwForwardDest = struct.unpack("I",socket.inet_aton(dwForwardDest))[0] #转换“0.0.0.0”格式的IP地址为网络序,注意不是主机字节序
pRoute.dwForwardMask =struct.unpack("I",socket.inet_aton(dwForwardMask))[0]
#如果不给定网关,则默认为默认路由的网关
pRoute.dwForwardNextHop = self.dwForwardNextHop
if dwForwardNextHop != None:
pRoute.dwForwardNextHop = struct.unpack("I",socket.inet_aton(dwForwardNextHop))[0]
pRoute.dwForwardPolicy = self.dwForwardPolicy
#如果不给定ForwardIfIndex,则默认为默认路由的接口
pRoute.dwForwardIfIndex=self.dwForwardIfIndex
if dwForwardIfIndex != None:
pRoute.dwForwardIfIndex=dwForwardIfIndex
pRoute.dwForwardType=self.dwForwardType
pRoute.dwForwardProto=self.dwForwardProto
pRoute.dwForwardAge=self.dwForwardAge
pRoute.dwForwardNextHopAS=self.dwForwardNextHopAS
# #win7及win10无法设定小于默认路由的Metric值,这里使用了相加。若不给定,最终与默认路由的Metric相同
pRoute.dwForwardMetric1=self.dwForwardMetric1+dwForwardMetric
pRoute.dwForwardMetric2=self.dwForwardMetric2
pRoute.dwForwardMetric3=self.dwForwardMetric3
pRoute.dwForwardMetric4=self.dwForwardMetric4
pRoute.dwForwardMetric5=self.dwForwardMetric5
dwStatus=ctypes.windll.iphlpapi.CreateIpForwardEntry(ctypes.byref(pRoute))
# print(type(dwStatus))
# print(dwStatus)
if dwStatus==5:
print("权限不足")
if dwStatus==5010:
print("已存在")
# if dwStatus==0:
# print ("添加路由成功")
return
def DeleteIpForwardEntry(self,dwForwardDest,dwForwardMask,
dwForwardNextHop=None,dwForwardMetric=0,dwForwardIfIndex=None):
DWORD=self.DWORD
NULL=self.NULL
NO_ERROR=self.NO_ERROR
bOrder=self.bOrder
ANY_SIZE=self.ANY_SIZE
pRoute=MIB_IPFORWARDROW() #建立结构域
#转换“0.0.0.0”格式的IP地址为网络序,注意不是主机字节序
pRoute.dwForwardDest = struct.unpack("I",socket.inet_aton(dwForwardDest))[0]
pRoute.dwForwardMask =struct.unpack("I",socket.inet_aton(dwForwardMask))[0]
# 如果不给定网关,则默认为默认路由的网关
pRoute.dwForwardNextHop = self.dwForwardNextHop
if dwForwardNextHop != None:
pRoute.dwForwardNextHop = struct.unpack("I",socket.inet_aton(dwForwardNextHop))[0]
pRoute.dwForwardPolicy = self.dwForwardPolicy
#如果不给定ForwardIfIndex,则默认为默认路由的接口
pRoute.dwForwardIfIndex=self.dwForwardIfIndex
if dwForwardIfIndex != None:
pRoute.dwForwardIfIndex = dwForwardIfIndex
pRoute.dwForwardType=self.dwForwardType
pRoute.dwForwardProto=self.dwForwardProto
pRoute.dwForwardAge=self.dwForwardAge
pRoute.dwForwardNextHopAS=self.dwForwardNextHopAS
#win7及win10无法设定小于默认路由的Metric值,这里使用了相加。若不给定,最终与默认路由的Metric相同
pRoute.dwForwardMetric1=self.dwForwardMetric1+dwForwardMetric
pRoute.dwForwardMetric2=self.dwForwardMetric2
pRoute.dwForwardMetric3=self.dwForwardMetric3
pRoute.dwForwardMetric4=self.dwForwardMetric4
pRoute.dwForwardMetric5=self.dwForwardMetric5
dwStatus=ctypes.windll.iphlpapi.DeleteIpForwardEntry(ctypes.byref(pRoute))
# print(type(dwStatus))
# print(dwStatus)
if dwStatus==5:
print("权限不足")
if dwStatus==5010:
print("不存在")
# if dwStatus==0:
# print ("删除路由成功")
return