获取windows 网卡GUID和ip信息

# coding: UTF-8
import _winreg

GUID=dict()
num = 0
netCfgInstanceID = None
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, \
                       r'System\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}')
keyInfo = _winreg.QueryInfoKey(hkey)

for index in range(keyInfo[0]):
    try:
        hSubKeyName = _winreg.EnumKey(hkey, index)
        if hSubKeyName == 'Properties':
            continue
        hSubKey = _winreg.OpenKey(hkey, hSubKeyName)
        
        hNdiInfKey = _winreg.OpenKey(hSubKey, r'Ndi\Interfaces')
        lowerRange = _winreg.QueryValueEx(hNdiInfKey, 'LowerRange')

    #获取网卡厂商名与网卡名、GUID
if lowerRange[0] == 'ethernet': driverDesc = _winreg.QueryValueEx(hSubKey, 'DriverDesc')[0] netCfgInstanceID = _winreg.QueryValueEx(hSubKey, 'NetCfgInstanceID')[0] network_name_key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, \ r'SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\%s\Connection'%str(netCfgInstanceID)) network_name = _winreg.QueryValueEx(network_name_key, 'Name')[0] if netCfgInstanceID == None: continue network_ip_list_key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, \ r'SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%s'%str(netCfgInstanceID))

    #获取ip,网关,子网掩码
try: network_ip_DefaultGateway = _winreg.QueryValueEx(network_ip_list_key, 'DefaultGateway')[0] network_ip_IPAddress = _winreg.QueryValueEx(network_ip_list_key, 'IPAddress')[0] network_ip_SubnetMask = _winreg.QueryValueEx(network_ip_list_key, 'SubnetMask')[0] except: network_ip_DefaultGateway = _winreg.QueryValueEx(network_ip_list_key, 'DhcpDefaultGateway')[0] network_ip_IPAddress = _winreg.QueryValueEx(network_ip_list_key, 'DhcpIPAddress')[0] network_ip_SubnetMask = _winreg.QueryValueEx(network_ip_list_key, 'DhcpSubnetMask')[0] GUID[num]={'network_name':network_name,'network_dev_name':driverDesc,'network_dev_GUID':netCfgInstanceID,'IPAddress':network_ip_IPAddress,'SubnetMask':network_ip_SubnetMask,'DefaultGateway':network_ip_DefaultGateway} num +=1 except: pass for i in GUID.keys(): print("network_name:\t\t%s"%GUID[i]['network_name']) print("network_dev_name:\t%s"%GUID[i]['network_dev_name']) print("network_dev_GUID:\t\Device\NPF_%s"%GUID[i]['network_dev_GUID']) if isinstance(GUID[i]['IPAddress'],list): print("IPAddress:\t\t%s"%'\n\t\t\t'.join(GUID[i]['IPAddress'])) else: print("IPAddress:\t\t%s"%GUID[i]['IPAddress']) if isinstance(GUID[i]['SubnetMask'],list): print("SubnetMask:\t\t%s"%'\n\t\t\t'.join(GUID[i]['SubnetMask'])) else: print("SubnetMask:\t\t%s"%GUID[i]['SubnetMask']) if isinstance(GUID[i]['DefaultGateway'],list): print("DefaultGateway:\t\t%s"%'\n\t\t\t'.join(GUID[i]['DefaultGateway'])) else: print("DefaultGateway:\t\t%s"%GUID[i]['DefaultGateway'])

network_name:        localnetwork
network_dev_name:    Realtek PCIe GBE Family Controller
network_dev_GUID:    \Device\NPF_{EDFEB7CD-306A-464A-92CA-655F95210CF3}
IPAddress:        172.17.7.18
SubnetMask:        255.255.240.0
DefaultGateway:        172.17.1.254

posted @ 2018-04-03 16:48  IT菜鸟园  阅读(2586)  评论(0编辑  收藏