Somputer
Somputer

Reputation: 1283

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?

Upvotes: 20

Views: 22012

Answers (7)

Naman Kumar
Naman Kumar

Reputation: 11

Here

the documentation of Scapy (scapy.interfaces.NetworkInterface) NetworkInterface class have multiple data variables

class NetworkInterface(object):
def __init__(self,
             provider,  # type: InterfaceProvider
             data=None,  # type: Optional[Dict[str, Any]]
             ):
    # type: (...) -> None
    self.provider = provider
    self.name = ""
    self.description = ""
    self.network_name = ""
    self.index = -1
    self.ip = None  # type: Optional[str]
    self.ips = defaultdict(list)  # type: DefaultDict[int, List[str]]
    self.mac = None  # type: Optional[str]
    self.dummy = False
    if data is not None:
        self.update(data)

here's how you can get name

from scapy.all import *
l=get_if_list()
dict=IFACES.data
for item in l:
    print(dic[item].name)

here's how you can get Driver name

from scapy.all import *
l=get_if_list()
dict=IFACES.data
for item in l:
    print(dic[item].description)

Upvotes: 1

Alex
Alex

Reputation: 827

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

Upvotes: 10

Dominic Nagel
Dominic Nagel

Reputation: 115

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

Upvotes: 0

Allen
Allen

Reputation: 1

It's a lot easier in PowerShell:

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

Upvotes: 0

Amit Shakya
Amit Shakya

Reputation: 1476

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>

Upvotes: 0

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210852

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

Upvotes: 1

Gord Thompson
Gord Thompson

Reputation: 123654

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

Upvotes: 25

Related Questions