Python获取计算机硬件信息:主板序列号、BIOS序列号、CPU序列号、硬盘序列号、网卡MAC地址
一、为何创作
在写python程序的过程中,有时需要获取计算机的主板序列号、bios序列号、CPU序列号、硬盘序列号、网卡网址等硬件信息,这方面的资料比较多,经查阅相关前辈资料,我将其进行了整合,供有需要者使用。
计算机的主板序列号、bios序列号、CPU序列号、硬盘序列号、网卡网址等硬件信息,可以用在软件保护中,如生成注册码、限制程序在特定授权机器上运行等。
二、程序说明
程序中调用了 wmi 和 uuid 两个库,如果这两个库没有安装,需在运行前安装,安装方法:(在命令行状态下运行)
pip install wmi
pip install uuid
wmi介绍
WMI的全称是Windows Management Instrumentation,即Windows管理规范。它是Windows操作系统上管理数据和操作的基础设施。我们可以使用WMI脚本或者应用自动化管理任务等。
在本文中,主要用于获取相关硬件序号等信息。
详细内容参考:https://blog.csdn.net/jgw2008/article/details/79095096
uuid 介绍
UUID (Universally Unique Identifier,通用唯一标识)是一个128位的用于计算机系统中以识别信息的数目,虽然生成UUID的概率不为零,但是无限接近零,因此可以忽略不记,如此一来,每个人都可以建立不与其他人冲突的UUID。
在本文中,主要获取网卡MAC地址信息。
详细内容参考:https://www.jb51.net/article/248285.htm
三、完整代码
写了一个class,可以调用其中一个或多个过程。直接运行get_all(),结果用print输出,结果如下:
可调用的程序如下:
主板序列号 get_board_id
bios序列号 get_bios_id
CPU序列号 get_cpu_id
硬盘序列号 get_physical_disk_id
所有网卡地址 get_all_mac
其中无线地址 get_MACAddress
其中有线地址 get_pc_mac_address (感觉有时不准确,希望有大侠可以纠正!谢谢啦!)
获得所有硬件信息 get_all
所有返回信息中的英文均大写。
完整代码如下:(复制后可以直接运行)
# encoding:utf-8
# Copyright (C) 2021-2024 Liu Qinghao
__author__ = 'Liu Qinghao'
import wmi # 硬件信息
import uuid # 网卡MAC地址用
class GetHardwareInformation:
'''
获取当前电脑 self.硬件信息,包括:
主板序列号 get_board_id
bios序列号 get_bios_id
CPU序列号 get_cpu_id
硬盘序列号 get_physical_disk_id
所有网卡地址 get_all_mac
其中无线地址 get_MACAddress
其中有线地址 get_pc_mac_address (不准确)
获得所有硬件信息 get_all
所有返回信息中的英文均大写
'''
def __init__(self) -> None:
self.硬件信息 = wmi.WMI()
def get_all_mac(self):
# mac地址
result=''
for mac in self.硬件信息.Win32_NetworkAdapter():
if mac.MACAddress is not None:
result += mac.MACAddress+' '
return result.strip().upper()
def get_MACAddress(self):
'''
获取无线网络MAC地址 返回大写地址,如:A8-8A-D2-7D-03-CD
'''
MACAddress = ''
for nw in self.硬件信息.Win32_NetworkAdapterConfiguration(IPEnabled=1):
MACAddress += nw.MACAddress +' '
return MACAddress.strip().upper()
def get_pc_mac_address(self):
"""
获取PC的Mac地址,返回大写地址,如:F8-A2-D6-CC-BB-AA
"""
mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
MACAddress = ":".join([mac[e:e + 2] for e in range(0, 11, 2)]).upper()
return MACAddress
def get_physical_disk_id(self):
# # 硬盘序列号
result=''
for physical_disk in self.硬件信息.Win32_DiskDrive():
result += physical_disk.SerialNumber+' '
return result.strip().upper()
def get_cpu_id(self):
# CPU序列号 获取费时间
result=''
for cpu in self.硬件信息.Win32_Processor():
result += cpu.ProcessorId.strip()+' '
return result.strip().upper()
def get_board_id(self):
# 主板序列号 获取速度快
result=''
for board_id in self.硬件信息.Win32_BaseBoard():
result += board_id.SerialNumber+' '
return result.strip().upper()
def get_bios_id(self):
# bios序列号 获取速度快
result=''
for bios_id in self.硬件信息.Win32_BIOS():
result += bios_id.SerialNumber+' '
return result.strip().upper()
def get_all(self):
# 列出所有硬件信息
result=(f'主板序列号:{self.get_board_id()}\nbios序列号:{self.get_bios_id()}\nCPU序列号:{self.get_cpu_id()}\n硬盘序列号:{self.get_physical_disk_id()}\n所有网卡网址:{self.get_all_mac()}\n其中无线网址:{self.get_MACAddress()}\n其中有线网址:{self.get_pc_mac_address()}')
print (result)
return result
if __name__ == '__main__':
a=GetHardwareInformation().get_all()
作者:刘庆豪2007