windows Python - 查询网络接口设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/594714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - query network interface setting
提问by DrFalk3n
My dream is to query the network card setting on WIN using python. What I need is to query if the NIC is set to full duplex and if it is set to 1000Mb full. WMI class named Win32_NetworkAdapterConfiguration doesn't provide these info.Alternatively a cmd command line sulution can be insteresting as well (via calling from python).
我的梦想是在WIN上用python查询网卡设置。我需要的是查询 NIC 是否设置为全双工以及是否设置为 1000Mb 满。名为 Win32_NetworkAdapterConfiguration 的 WMI 类不提供这些信息。或者,cmd 命令行 sulution 也很有趣(通过从 python 调用)。
Thnx
谢谢
回答by Paul Stephenson
As @ironfroggy said, this is not specific to Python, but a general Windows question.
正如@ironfroggy 所说,这不是 Python 特有的,而是一个普遍的 Windows 问题。
When we wanted to programmatically find the speed and duplex settings on network cards, it was very difficult. In the end we resorted to wandering through the registry, which has a different structure depending on the vendor of your NIC.
当我们想以编程方式查找网卡上的速度和双工设置时,非常困难。最后,我们求助于注册表,它具有不同的结构,具体取决于您的 NIC 供应商。
It goes something like this; apologies for any errors:
它是这样的;对任何错误表示歉意:
- Find
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network
and look for GUIDs that have the data "Network Adapters". Call this<GUID1>
. - Underneath the
<GUID1>
key is another GUID for each NIC. Call one of them<GUID2>
. - Go to
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\<GUID1>
. - Iterate through its keys (they look like 0000, 0001, 0002, etc.) until you find one that has the value
NetCfgInstanceId
equal to<GUID2>
. - Underneath here, look for a subkey dependent on NIC vendor. Some we have defined are:
- Ndi\Params
- Ndi\savedParams
- BRCMndi\params
- Under there, iterate through the keys until you find one with a value called
ParamDesc
whose data contains the words "speed" and "duplex". Remember the key name and called it<SpeedDuplexParamName>
. - Underneath
<SpeedDuplexParamName>
there is anenum
key, which matches numbers to descriptions like "Auto-detect" and "100 Mb Full". - Go back up a few levels to where you found
NetCfgInstanceId
. Near there you can see the current value as an enumeration. For one example here the value name wasRequestedMediaType
and the value was 6. - Look up the enumeration value to find the speed and duplex setting.
- 查找
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network
并查找具有“网络适配器”数据的 GUID。调用这个<GUID1>
。 <GUID1>
密钥下方是每个 NIC 的另一个 GUID。呼叫其中之一<GUID2>
。- 去
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\<GUID1>
。 - 遍历它的键(它们看起来像 0000、0001、0002 等),直到找到一个值
NetCfgInstanceId
等于 的键<GUID2>
。 - 在此处下方,查找依赖于 NIC 供应商的子项。我们定义的一些是:
- Ndi\参数
- Ndi\savedParams
- BRCMndi\参数
- 在那里,遍历键,直到找到一个值称为
ParamDesc
“速度”和“双工”的数据。记住键名并调用它<SpeedDuplexParamName>
。 - 下面
<SpeedDuplexParamName>
有一个enum
键,它将数字与“自动检测”和“100 Mb 满”等描述相匹配。 - 返回几个级别到您找到的位置
NetCfgInstanceId
。在那里你可以看到当前值作为一个枚举。举个例子,这里的值名称是RequestedMediaType
,值是 6。 - 查找枚举值以找到速度和双工设置。
I see @DrFalk3n has linked to a Microsoft article that might say the same, but I'll leave this here in case it is helpful.
我看到@DrFalk3n 已链接到 Microsoft 的一篇文章,该文章可能会说相同的内容,但我会将其留在这里以防万一。
回答by ironfroggy
This really isn't a python question. Anything you can do with the win32api, You can do through Python, so the question here is just about the windows API and it doesn't make any difference that you happen to be invoking them from Python. Asking without the Python tag might get better response, actually, because people will look who know how to do it in another language, which would be using the same API.
这真的不是一个python问题。你可以用 win32api 做的任何事情,你都可以通过 Python 做,所以这里的问题只是关于 windows API,你碰巧从 Python 调用它们没有任何区别。实际上,在没有 Python 标签的情况下询问可能会得到更好的回应,因为人们会寻找知道如何用另一种语言来做这件事的人,这将使用相同的 API。
回答by DrFalk3n
Yes! This really isn't a python question. Anyway take a look at: List Network Adapter Configuration Properties
是的!这真的不是一个python问题。无论如何看看: 列出网络适配器配置属性
and brows down into the python@microsoft scripts repositorymaybe helpfull for your problem.
并向下浏览 python@microsoft 脚本存储库可能对您的问题有所帮助。
You can also find usefull in python:
你也可以在 python 中找到有用的:
>>> import os
>>> f= os.popen('<type your dos command>')
>>> s = f.read()
>>> print s