Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I use the netifaces module.

import netifaces
print netifaces.interfaces()

but this shows the result below:

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

I want to get a "friendly" interface name like "Local Area Connection" in Windows.

How can I get that?

like image 747
Somputer Avatar asked Apr 28 '15 07:04

Somputer


People also ask

How do I list an interface in Windows?

You can launch a command prompt by pressing "Windows Key-R," typing "cmd" and pressing "Enter." Select the command prompt window, type the command "route print" and press "Enter" to display the "Interface List" and system routing tables.


2 Answers

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']
like image 116
Gord Thompson Avatar answered Sep 16 '22 13:09

Gord Thompson


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'}
]
like image 41
Alex Avatar answered Sep 19 '22 13:09

Alex