How can I programmatically detect a Window's n

2019-08-12 22:36发布

问题:

I am using WMI to detect a number of items about a network adapter's state. Among the things I need to find out are (a) speed and (b) duplex.

I have been able to detect the speed of a network adapter by using WMI and the following Python code:

from pycom.client import wmi

dev_name = r"\\DEVICE\\{287EB4BB-5C2A-4108-B377-15E1D0B0E760}"
query1 = """
SELECT * 
FROM  MSNdis_EnumerateAdapter
WHERE DeviceName = '%s'""" % dev_name


wmi_ndis = wmi.WMI("root\\WMI")
results = wmi_ndis.ExecQuery(query1)
instance_name = results[0].InstanceName

del results

query2="""
SELECT * 
FROM MSNdis_LinkSpeed
WHERE InstanceName='%s'""" % instance_name
results = wmi_ndis.ExecQuery(query2)
linkspeed = results[0].NdisLinkSpeed

del results

print instance_name, linkspeed

del instance_name
del linkspeed
del wmi_ndis

There appears to be a perfect class for the data I want: MSNDis_LinkParameters. However, this table does not appear to be populated. There are values in Win32_NetworkAdapter as well, but they are also not populated.

I would be happy to use a native C API or WMI, but I can't do screen scraping because the application needs to work with arbitrary languages. Thanks!

回答1:

Apparently the underlying issue here is that WMI provider implementation is handled by the NIC vendor, not the OS-- so some NICs may support some settings while (as you've discovered) others don't.

For link speed, check out this for some WMI scripts which may work on most NICs.

For duplex, I think you're out of luck, at least according to topic. Look at the last post in that thread-- it seems pretty specific about how to work around the limit in some cases, but won't work for all NICs.