一、WMI
WMI(Windows Management Instrumentation,系统管理规范)是微软基于Web的企业管理实施,它提供了几乎任何有关计算机系统的信息。在Windows系统中,在命令窗口输入
wmic CPU get ProcessorID
可以获得CPU序列号。Python的WMI模块是在pywin32扩展基础上的一个轻量级包装,隐藏了一些Python与WMI API对话所需的杂乱的管道。
二、WMI示例
Python用WMI模块可以很方便地回去Windows系统的硬件信息:CPU、硬盘主板等。
import wmi
def PrintDiskInfo():
c = wmi.WMI()
#cpu序列号
for cpu in c.Win32_Processor():
print "cpu id:", cpu.ProcessorId.strip()
#硬盘序列号
for disk in c.Win32_DiskDrive():
print 'disk id:', disk.SerialNumber.strip()
#主板序列号
for board in c.Win32_BaseBoard():
print "main board id:", board.SerialNumber.strip()
#mac地址
for networkAdapter in c.Win32_NetworkAdapterConfiguration (IPEnabled = True):
print "mac address:", networkAdapter.MACAddress
#bios序列号
for bios in c.Win32_BIOS():
print "bios number:", bios.SerialNumber.strip()
if __name__ == "__main__":
PrintDiskInfo()
三、常见问题
1、WMI获取的CPU序列号是否唯一?
不是唯一。实际编程中,发现同一批电脑的cpu.ProcessorId可能一模一样。对此,微软在MSDN上有相关说明:
http://www.blogs8.cn/posts/TNR434。截图如下:
2、主板序列号是否一定可以获取到?
不一定。可能为None。