每日一模块-获取电脑的mac地址
wmi模块
import wmi
w = wmi.WMI()
for n in w.Win32_NetworkAdapter():
if n.MACADDress is None or n.PhysicalAdapter is False:
continue
mac = n.MACADDress
name = n.Name
if "PCI" in name:
# 有线网卡
print("%s: %s" % (name, mac))
wmi获取当前在使用的网卡
def get_pc_mac_address():
"""
获取电脑mac地址
:return: 电脑mac地址,如:f8a2d60123456
"""
print('\n========= 获取电脑mac地址 =========')
global s
import wmi
s = wmi.WMI()
network = []
for nw in s.Win32_NetworkAdapterConfiguration(IPEnabled=1):
network.append(
{
"MAC": nw.MACAddress,
"ip": nw.IPAddress
}
)
MACAddress = str(nw.MACAddress).replace(':', '')
print("MACAddress={}".format(MACAddress))
return MACAddress
uuid获取的pcmac
def get_pc_mac_address_upper():
"""
获取PC的Mac地址,返回大写地址,如:F8-A2-D6-CC-BB-AA
:return:
"""
import uuid
print('\n========= 获取电脑mac地址 =========')
mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
# 转大写
mac_1 = "-".join([mac[e:e + 2] for e in range(0, 11, 2)]).upper()
print('pc_mac_1={}'.format(mac_1))
return mac_1
获取硬盘序列号、主板序列号等
# -*- coding: utf-8 -*-
import wmi
class Hardware:
@staticmethod
def get_cpu_sn():
"""
获取CPU序列号
:return: CPU序列号
"""
c = wmi.WMI()
for cpu in c.Win32_Processor():
# print(cpu.ProcessorId.strip())
return cpu.ProcessorId.strip()
@staticmethod
def get_baseboard_sn():
"""
获取主板序列号
:return: 主板序列号
"""
c = wmi.WMI()
for board_id in c.Win32_BaseBoard():
# print(board_id.SerialNumber)
return board_id.SerialNumber
@staticmethod
def get_bios_sn():
"""
获取BIOS序列号
:return: BIOS序列号
"""
c = wmi.WMI()
for bios_id in c.Win32_BIOS():
# print(bios_id.SerialNumber.strip)
return bios_id.SerialNumber.strip()
@staticmethod
def get_disk_sn():
"""
获取硬盘序列号
:return: 硬盘序列号列表
"""
c = wmi.WMI()
disk_sn_list = []
for physical_disk in c.Win32_DiskDrive():
# print(physical_disk.SerialNumber)
# print(physical_disk.SerialNumber.replace(" ", ""))
disk_sn_list.append(physical_disk.SerialNumber.replace(" ", ""))
return disk_sn_list
if __name__ == '__main__':
print("CPU序列号:{}".format(Hardware.get_cpu_sn()))
print("主板序列号:{}".format(Hardware.get_baseboard_sn()))
print("Bios序列号:{}".format(Hardware.get_bios_sn()))
print("硬盘序列号:{}".format(Hardware.get_disk_sn()))
人生苦短,我用python!