# coding: UTF-8 import _winreg GUID=dict() num = 0 netCfgInstanceID = None hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SystemCurrentControlSetControlClass{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'NdiInterfaces') 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'SYSTEMCurrentControlSetControlNetwork{4D36E972-E325-11CE-BFC1-08002BE10318}\%sConnection'%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'SYSTEMCurrentControlSetservicesTcpipParametersInterfaces\%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: %s"%GUID[i]['network_name']) print("network_dev_name: %s"%GUID[i]['network_dev_name']) print("network_dev_GUID: DeviceNPF_%s"%GUID[i]['network_dev_GUID']) if isinstance(GUID[i]['IPAddress'],list): print("IPAddress: %s"%' '.join(GUID[i]['IPAddress'])) else: print("IPAddress: %s"%GUID[i]['IPAddress']) if isinstance(GUID[i]['SubnetMask'],list): print("SubnetMask: %s"%' '.join(GUID[i]['SubnetMask'])) else: print("SubnetMask: %s"%GUID[i]['SubnetMask']) if isinstance(GUID[i]['DefaultGateway'],list): print("DefaultGateway: %s"%' '.join(GUID[i]['DefaultGateway'])) else: print("DefaultGateway: %s"%GUID[i]['DefaultGateway'])
network_name: localnetwork
network_dev_name: Realtek PCIe GBE Family Controller
network_dev_GUID: DeviceNPF_{EDFEB7CD-306A-464A-92CA-655F95210CF3}
IPAddress: 172.17.7.18
SubnetMask: 255.255.240.0
DefaultGateway: 172.17.1.254