import winreg
import netifaces
def_gw_device = netifaces.gateways()['default'][netifaces.AF_INET][1]
# 打开注册表键
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}")
# 遍历所有子键
for i in range(winreg.QueryInfoKey(key)[0]):
# 获取子键名称
subkey_name = winreg.EnumKey(key, i)
try:
# 打开子键
subkey = winreg.OpenKey(key, subkey_name)
netCfgInstanceId = winreg.QueryValueEx(subkey, "NetCfgInstanceId")[0]
driverDesc = winreg.QueryValueEx(subkey, "DriverDesc")[0]
if netCfgInstanceId == def_gw_device:
print("当前在使用的网卡编号为:", subkey_name)
print("网卡名称:", driverDesc)
except (FileNotFoundError, PermissionError):
# 如果没有 DriverDesc 值或拒绝访问,跳过
pass
# 关闭注册表键
winreg.CloseKey(key)