raphdg / netifaces

I'm not the author of netifaces, I just added the project on github and I added a spec file to build the RPM package for the basic Amazon Linux AMI 2012.03
35 stars 10 forks source link

unintelligible nic names on windows 10? #6

Open robertsdotpm opened 2 years ago

robertsdotpm commented 2 years ago

First, let me say I have no idea whose repo to contribute to for netifaces. The one that pypy lists as the Github project is now 'archived' and no one can contribute to it. I don't know why this was done but I feel it was a bad decision. This project is very useful and multiple people have tried to contribute stuff!

Anyway, NIC names for interfaces on windows are basically just hex strings. The most intelligible data you have for NIC on Windows seems to be the description where you have something like 'Realtek Gaming 2.5GbE Family Controller' instead of '{B2FE597D-E4C7-40E6-960B-E4BFEB88062F}'. I have too much to do to write a pull request but I wanted to provide a simple function that lets you convert a description to a NIC id which can then be used with the netifaces code. Here it is:

def nt_reg_nic(value, desc=0, guid=0):
    import winreg

    nic_keys = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards')
    nic_no, _, _ = winreg.QueryInfoKey(nic_keys)
    for nic_offset in range(0, nic_no):
        nic_key = None
        try:
            nic_key_name = winreg.EnumKey(nic_keys, nic_offset)
            nic_key = winreg.OpenKey(nic_keys, nic_key_name)
            nic_desc, _ = winreg.QueryValueEx(nic_key, "Description")
            nic_guid, _ = winreg.QueryValueEx(nic_key, "ServiceName")

            if desc:
                if value in nic_desc:
                    winreg.CloseKey(nic_keys)
                    return nic_guid 
            if guid:
                if value in nic_guid:
                    winreg.CloseKey(nic_keys)
                    return nic_desc
        except:
            continue
        finally:
            if nic_key is not None:
                winreg.CloseKey(nic_key)

    winreg.CloseKey(nic_keys)
    return None

nt_reg_nic("Realtek Gaming 2.5GbE Family Controller", desc=1) == '{B2FE597D-E4C7-40E6-960B-E4BFEB88062F}'
nt_reg_nic('{B2FE597D-E4C7-40E6-960B-E4BFEB88062F}', guid=1) == "Realtek Gaming 2.5GbE Family Controller"

By the way: to get NIC descriptions for use with this function you can type route print into CMD. This command will show an interface list that has: [nic_no, nic_mac, nic_description] -- the 3rd column is what you can use for the above function. Note also that there's a lot of garbage in this output. I.E. interfaces that might not even have IPs, are virtual interfaces (mine has VBOX interfaces), and so on. As far as I know netifaces also shows all such interfaces on Windows but has some nice functions to show what the 'default' interfaces are for IPv4 and IPv6 routing. So engineers might want to check out that. Cheers.