forked from Shinlor/WinRoute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_wmi.py
72 lines (66 loc) · 2.15 KB
/
test_wmi.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
import wmi # install wmi & pywin32
# c = wmi.WMI()
# for interface in c.Win32_NetworkAdapterConfiguration(IPEnabled=1):
# print(interface.Description)
# print(interface.MACAddress)
# for ip_address in interface.IPAddress:
# print (ip_address)
# print
def getGateway():
c = wmi.WMI()
configList = c.Win32_NetworkAdapterConfiguration(
Description="Qualcomm Atheros QCA9377 Wireless Network Adapter",
IPEnabled=1)
configList = c.Win32_NetworkAdapterConfiguration(IPEnabled=1)
print(type(configList)) # list
print(len(configList))
if len(configList) > 1:
print('get more than 1 adapter')
# return
for conf in configList:
print(conf.Description)
conf = configList[0]
# print(c.Index)
print(conf.InterfaceIndex)
print(type(conf.DefaultIPGateway)) # tuple
print(conf.DefaultIPGateway)
return
def getDefaultGateWayFromRouteTable():
itm_dict = {'InterfaceIndex': -1, 'NextHop': '192.168.1.1'}
# dictList = []
minMetric = 10000
routeList = wmi.WMI().Win32_IP4RouteTable()
# print(len(routeList))
if len(routeList) > 50:
err = 'route item number exceed !!\n'
err += 'check your route table by cmd: route print'
raise Exception(err)
for item in routeList:
# if item.Destination
# print(item)
# print(type(item.Destination)) # str
# if item.Destination.equals('0.0.0.0'):
if item.Destination == '0.0.0.0' and item.Destination == '0.0.0.0':
print('find 0.0.0.0/0')
if (item.Metric1 < minMetric):
itm_dict['InterfaceIndex'] = item.InterfaceIndex
itm_dict['NextHop'] = item.NextHop
# itm_dict = {
# 'InterfaceIndex': item.InterfaceIndex,
# 'NextHop': item.NextHop
# }
# dictList.append(itm_dict)
# print(itm_dict)
if itm_dict['InterfaceIndex'] == -1:
err = 'getDefaultGateWayFromRouteTable fail !\n'
err += 'check your route table by cmd: route print'
# print(err)
raise Exception(err)
return itm_dict
if __name__ == '__main__':
# main()
# getGateway()
getDefaultGateWayFromRouteTable()
# gateway
# <https://blog.csdn.net/unsv29/article/details/82148344>
# <https://my.oschina.net/yushulx/blog/484663>