How to get meaningful network interface names instead of GUIDs with netifaces under Windows?

前端 未结 6 795
天涯浪人
天涯浪人 2020-12-05 18:52

I use the netifaces module.

import netifaces
print netifaces.interfaces()

but this shows the result below:

 [\         


        
相关标签:
6条回答
  • 2020-12-05 19:30

    If you know the IP Address which your Interface uses, you could simply do something like this:

    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
    
    0 讨论(0)
  • 2020-12-05 19:31

    We can also use 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)
    

    yields on my machine:

    ['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

    0 讨论(0)
  • 2020-12-05 19:43

    The Scapy module has a built in get_windows_if_list() that works well. (I shortened the output a bit)

    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'}
    ]
    
    0 讨论(0)
  • 2020-12-05 19:50

    Extending on MaxU answer

    More accurate/refined way

    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
    

    and then fire a query with respect to

    SELECT * FROM Win32_NetworkAdapter where index= <Index>
    
    0 讨论(0)
  • 2020-12-05 19:53

    It looks like netifaces leaves it up to us to pull the information out of the Windows Registry. The following functions work for me under Python 3.4 on Windows 8.1.

    To get the connection name ...

    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))
    

    .. which on my machine produces:

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

    To get the driver name ...

    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))
    

    ... which gives me:

    ['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']
    
    0 讨论(0)
  • 2020-12-05 19:53

    It's a lot easier in PowerShell:

    Get-WmiObject -Class Win32_NetworkAdapterConfiguration | 
        Select-Object Description, SettingID, MACAddress | 
        Format-Table -AutoSize
    
    0 讨论(0)
提交回复
热议问题