如何在Windows下获取有意义的网络接口名称而不是带有netifaces的GUID?

时间:2015-04-28 07:53:09

标签: 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}',
'{B5DC7787-26DC-4540-8424-A1D5598DC175}']

我希望得到一个友好的"接口名称如" Local Area Connection"在Windows中。

我怎么能得到它?

6 个答案:

答案 0 :(得分:18)

看起来netifaces让我们将信息从Windows注册表中提取出来。以下函数适用于Windows 8.1上的Python 3.4。

获取连接名称...

import netifaces as ni
import winreg as wr
from pprint import pprint

def get_connection_name_from_guid(iface_guids):
    iface_names = ['(unknown)' for i in range(len(iface_guids))]
    reg = wr.ConnectRegistry(None, wr.HKEY_LOCAL_MACHINE)
    reg_key = wr.OpenKey(reg, r'SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}')
    for i in range(len(iface_guids)):
        try:
            reg_subkey = wr.OpenKey(reg_key, iface_guids[i] + r'\Connection')
            iface_names[i] = wr.QueryValueEx(reg_subkey, 'Name')[0]
        except FileNotFoundError:
            pass
    return iface_names

x = ni.interfaces()
pprint(get_connection_name_from_guid(x))

..我机器上的产品:

['Local Area Connection* 12',
 'Bluetooth Network Connection',
 'Wi-Fi',
 'Ethernet',
 'VirtualBox Host-Only Network',
 '(unknown)',
 'isatap.{4E4150B0-643B-42EA-AEEA-A14FBD6B1844}',
 'isatap.{BB05D283-4CBF-4514-B76C-7B7EBB2FC85B}']

获取驱动程序名称...

import netifaces as ni
import winreg as wr
from pprint import pprint

def get_driver_name_from_guid(iface_guids):
    iface_names = ['(unknown)' for i in range(len(iface_guids))]
    reg = wr.ConnectRegistry(None, wr.HKEY_LOCAL_MACHINE)
    reg_key = wr.OpenKey(reg, r'SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}')
    for i in range(wr.QueryInfoKey(reg_key)[0]):
        subkey_name = wr.EnumKey(reg_key, i)
        try:
            reg_subkey = wr.OpenKey(reg_key, subkey_name)
            guid = wr.QueryValueEx(reg_subkey, 'NetCfgInstanceId')[0]
            try:
                idx = iface_guids.index(guid)
                iface_names[idx] = wr.QueryValueEx(reg_subkey, 'DriverDesc')[0]
            except ValueError:
                pass
        except PermissionError:
            pass
    return iface_names

x = ni.interfaces()
pprint(get_driver_name_from_guid(x))

......这给了我:

['Microsoft Wi-Fi Direct Virtual Adapter',
 'Bluetooth Device (Personal Area Network)',
 'Dell Wireless 1395 WLAN Mini-Card',
 'Broadcom 440x 10/100 Integrated Controller',
 'VirtualBox Host-Only Ethernet Adapter',
 '(unknown)',
 'Microsoft ISATAP Adapter',
 'Microsoft ISATAP Adapter']

答案 1 :(得分:2)

Scapy模块具有内置的get_windows_if_list(),可以正常工作。 (我将输出缩短了一点)

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'}
]

答案 2 :(得分:0)

我们也可以使用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']

MSDN: Win32_NetworkAdapter class

答案 3 :(得分:0)

扩展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

然后触发关于

的查询
SELECT * FROM Win32_NetworkAdapter where index= <Index>

答案 4 :(得分:0)

PowerShell更容易:

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

答案 5 :(得分:0)

如果您知道接口使用的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