今天 使用 pcap库 选网卡发现 findalldevs() 调出来的网卡基本上不可读
那么我们能不能将这个值转换为我们可读的值呢
这里参考了以下代码
import platform
if 'Windows' in platform.platform():
import winreg as wr
IF_REG = r'SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}'
##这个函数就是将网卡的名字转换为 findalldevs 返回的值
def getInterfaceByName(name):
'''Get guid of interface from regedit of windows system
Args:
name: interface name
Returns:
An valid guid value or None.
Example:
getInterfaceByName('eth0')
'''
reg = wr.ConnectRegistry(None, wr.HKEY_LOCAL_MACHINE)
reg_key = wr.OpenKey(reg, IF_REG)
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 + r'\Connection')
Name = wr.QueryValueEx(reg_subkey, 'Name')[0]
wr.CloseKey(reg_subkey)
if Name == name:
return r'\Device\NPF_' + subkey_name
except FileNotFoundError as e:
pass
return None
这里直接上代码
import platform
if 'Windows' in platform.platform():
import winreg as wr
IF_REG = r'SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}'
def FindalldevsName(devs):
reg = wr.ConnectRegistry(None, wr.HKEY_LOCAL_MACHINE)
reg_key = wr.OpenKey(reg, IF_REG)
try:
reg_subkey = wr.OpenKey(reg_key, devs + r'\Connection')
return wr.QueryValueEx(reg_subkey, 'Name')[0]
except FileNotFoundError as e:
...
return None
findOb = = pcap.findalldevs()
for i in name:
FindalldevsName(i.split('_')[1])