Python 如何在Windows下获得有意义的网络接口名称,而不是带有netifaces的GUI?

Python 如何在Windows下获得有意义的网络接口名称,而不是带有netifaces的GUI?,python,python-2.7,Python,Python 2.7,我使用netifaces模块 import netifaces print netifaces.interfaces() 但结果如下: ['{CDC97813-CC28-4260-BA1E-F0CE3081DEC7}', '{846EE342-7039-11DE-9D20-806E6F6E6963}', '{A51BA5F0-738B-4405-975F-44E67383513F}', '{A646FA85-2EC6-4E57-996E-96E1B1C5CD59}', '{B5DC77

我使用
netifaces
模块

import netifaces
print netifaces.interfaces()
但结果如下:

 ['{CDC97813-CC28-4260-BA1E-F0CE3081DEC7}',
 '{846EE342-7039-11DE-9D20-806E6F6E6963}',
 '{A51BA5F0-738B-4405-975F-44E67383513F}',
 '{A646FA85-2EC6-4E57-996E-96E1B1C5CD59}',
'{B5DC7787-26DC-4540-8424-A1D5598DC175}']
我想在Windows中获得“友好”的界面名称,如“局域网连接”


我怎样才能得到它呢?

看起来
netifaces
让我们从Windows注册表中提取信息。以下函数在Windows8.1上的Python3.4下适用

要获取连接名称

将面导入为ni
将winreg作为wr导入
从pprint导入pprint
def get_connection_name_from_guid(iface_guid):
iface_名称=['(未知)'表示范围内的i(len(iface_GUID))]
reg=wr.ConnectRegistry(无,wr.HKEY\u本地\u机器)
reg_key=wr.OpenKey(reg,r'SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318})
对于范围内的i(len(iface_guids)):
尝试:
注册表子项=wr.OpenKey(注册表项,iface\u guids[i]+r'\Connection')
iface_names[i]=wr.queryvaluex(reg_子键,'Name')[0]
除FileNotFoundError外:
通过
返回iface\u名称
x=ni.interfaces()
pprint(从guid(x)获取连接名)
。。在我的机器上产生:

[“本地连接*12”,
“蓝牙网络连接”,
“Wi-Fi”,
“以太网”,
“仅限VirtualBox主机的网络”,
"(未知)",,
“isatap.{4E4150B0-643B-42EA-AEEA-A14FBD6B1844}”,
'isatap.{BB05D283-4CBF-4514-B76C-7B7EBB2FC85B}']
要获取驱动程序名称

将面导入为ni
将winreg作为wr导入
从pprint导入pprint
def从guid获取驱动程序名称(iface guid):
iface_名称=['(未知)'表示范围内的i(len(iface_GUID))]
reg=wr.ConnectRegistry(无,wr.HKEY\u本地\u机器)
reg_key=wr.OpenKey(reg,r'SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318})
对于范围内的i(wr.QueryInfoKey(reg_key)[0]):
子项名称=wr.EnumKey(注册表项,i)
尝试:
reg_subkey=wr.OpenKey(reg_key,subkey\u name)
guid=wr.QueryValueX(注册表子项'NetCfgInstanceId')[0]
尝试:
idx=iface\u guids.index(guid)
iface_names[idx]=wr.queryValue(注册表子项'DriverDesc')[0]
除值错误外:
通过
除许可错误外:
通过
返回iface\u名称
x=ni.interfaces()
pprint(从guid(x)获取驱动程序名)
。。。这给了我:

[“Microsoft Wi-Fi直接虚拟适配器”,
“蓝牙设备(个人局域网)”,
“Dell Wireless 1395 WLAN迷你卡”,
“Broadcom 440x 10/100集成控制器”,
“仅限VirtualBox主机的以太网适配器”,
"(未知)",,
“Microsoft ISATAP适配器”,
“Microsoft ISATAP适配器”]

我们也可以使用Windows WMI:

import wmi

c = wmi.WMI()
qry = "select Name from Win32_NetworkAdapter where NetEnabled=True and NetConnectionStatus=2"

lst = [o.Name for o in c.query(qry)]
print(lst)
在我的机器上:

['Realtek PCIe GBE Family Controller', 'VMware Virtual Ethernet Adapter for VMnet1', 'VMware Virtual Ethernet Adapter for VMnet8', 'VirtualBox Host-Only Ethernet Adapter']

扩展MaxU答案

更精确/精确的方法

Select Index from Win32_NetworkAdapterConfiguration WHERE IPEnabled = True or (ServiceName<>'' 
                                                    AND ServiceName<>'AsyncMac' 
                                                    AND ServiceName<>'VMnetx' AND ServiceName<>'VMnetadapter' AND ServiceName<>'Rasl2tp' AND ServiceName<>'msloop' AND ServiceName<>'PptpMiniport' 
                                                    AND ServiceName<>'Raspti' AND ServiceName<>'NDISWan' AND ServiceName<>'NdisWan4' AND ServiceName<>'RasPppoe' 
                                                    AND ServiceName<>'NdisIP' AND Description<>'PPP Adapter.') AND MACAddress is not NULL
从Win32\u NetworkAdapter配置中选择索引,其中IPEnabled=True或(ServiceName“”
和ServiceName'AsyncMac'
以及ServiceName'VMnetx'和ServiceName'VMnetadapter'和ServiceName'Rasl2tp'和ServiceName'msloop'和ServiceName'PptpMiniport'
以及ServiceName'Raspti'和ServiceName'NDISWan'和ServiceName'NdisWan4'和ServiceName'RasPppoe'
和ServiceName'NdisIP'和Description'PPP Adapter.')和MACAddress不为空
然后激发一个关于

SELECT * FROM Win32_NetworkAdapter where index= <Index>
从Win32\u NetworkAdapter中选择*,其中索引=

在PowerShell中要容易得多:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration | 
    Select-Object Description, SettingID, MACAddress | 
    Format-Table -AutoSize

如果您知道接口使用的IP地址,您可以简单地执行以下操作:

import netifaces as ni
def get_interfaces():
    ifaces = ni.interfaces()
    for iface in ifaces:
        try:
            ip = ni.ifaddresses(iface)[ni.AF_INET][0]["addr"]
            print(f"IP: {ip} from Interface {iface}")
        except:
            pass

Scapy模块有一个内置的
get\u windows\u if\u list()
,运行良好。(我把输出缩短了一点)

win32上的Python 3.7.0(v3.7.0:1bf9cc5093,2018年6月27日,04:59:51)[MSC v.1914 64位(AMD64)] 有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。 >>>从斯卡皮来的,都是进口货* >>>获取\u窗口\u如果\u列表() [ {'name':'Realtek USB GbE系列控制器','win_index':'17','description':'Ethernet','guid','mac','netid','Ethernet'}, {'name':'Intel(R)双频无线AC 8260','win_index':'5','description':'Wi-Fi','guid','mac','netid','Wi-Fi'} ]
这甚至可以在Windows XP上运行,这是非常不寻常的。这非常有用。回答如下:。请参阅关于psutil的帖子。
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from scapy.all import *
>>> get_windows_if_list()
[
    {'name': 'Realtek USB GbE Family Controller', 'win_index': '17', 'description': 'Ethernet', 'guid': '<guid>', 'mac': '<mac>', 'netid': 'Ethernet'},  
    {'name': 'Intel(R) Dual Band Wireless-AC 8260', 'win_index': '5', 'description': 'Wi-Fi', 'guid': '<guid>', 'mac': '<mac>', 'netid': 'Wi-Fi'}
]