importwmi
############ 1. 获取硬件信息,输出 macode#1.CPU序列号(ID) 2.本地连接 无线局域网 以太网的MAC 3.硬盘序列号(唯一) 4.主板序列号(唯一)
globals
s=wmi.WMI()#cpu 序列号
defget_CPU_info(self):
cpu=[]
cp=s.Win32_Processor()for u incp:
cpu.append(
{"Name": u.Name,"Serial Number": u.ProcessorId,"CoreNum": u.NumberOfCores
}
)#print(":::CPU info:", json.dumps(cpu))
returncpu#硬盘序列号
defget_disk_info(self):
disk=[]for pd ins.Win32_DiskDrive():
disk.append(
{"Serial": s.Win32_PhysicalMedia()[0].SerialNumber.lstrip().rstrip(), #获取硬盘序列号,调用另外一个win32 API
"ID": pd.deviceid,"Caption": pd.Caption,"size": str(int(float(pd.Size)/1024/1024/1024))+"G"}
)#print(":::Disk info:", json.dumps(disk))
returndisk#mac 地址(包括虚拟机的)
defget_network_info(self):
network=[]for nw in s.Win32_NetworkAdapterConfiguration (): #IPEnabled=0
if nw.MACAddress !=None:
network.append(
{"MAC": nw.MACAddress, #无线局域网适配器 WLAN 物理地址
"ip": nw.IPAddress
}
)#print(":::Network info:", json.dumps(network))
returnnetwork#主板序列号
defget_mainboard_info(self):
mainboard=[]for board_id ins.Win32_BaseBoard ():
mainboard.append(board_id.SerialNumber.strip().strip("."))returnmainboard#由于机器码太长,故选取机器码字符串部分字符#E0:DB:55:B5:9C:16BFEBFBFF00040651W3P0VKEL6W8T1Z1.CN762063BN00A8#1 61 k 8Z#machinecode_str = ""#machinecode_str = machinecode_str+a[0]["MAC"]+b[0]["Serial Number"]+c[0]["Serial"]+d[0]
defgetCombinNumber(self):
a=self.get_network_info()
b=self.get_CPU_info()
c=self.get_disk_info()
d=self.get_mainboard_info()
machinecode_str= ""machinecode_str= machinecode_str+a[0]["MAC"]+b[0]["Serial Number"]+c[0]["Serial"]+d[0]
selectindex=[15,30,32,38,43,46]
macode=""
for i inselectindex:
macode=macode+machinecode_str[i]### print(macode)
return macode