天天看点

10、Python获取window10 配置信息

import wmi
import re
import win32com
############ 1. 获取硬件信息,输出 macode
#   1.CPU序列号(ID) 2.本地连接 无线局域网 以太网的MAC 3.硬盘序列号(唯一) 4.主板序列号(唯一)


s = wmi.WMI()
def get_memory_info():
    data = []
    # 这个模块用SQL语言获取数据
    wmi_service_obj = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    wmi_service_connector = wmi_service_obj.ConnectServer(".", "root\cimv2")
    ram_collections = wmi_service_connector.ExecQuery("Select * from Win32_PhysicalMemory")
    for ram in ram_collections:  # 主机中存在很多根内存,要循环所有的内存数据
        ram_size = int(int(ram.Capacity) / (1024 ** 3))  # 转换内存单位为GB
        item_data = {
            "slot": ram.DeviceLocator.strip(),
            "capacity": ram_size,
            "model": ram.Caption,
            "manufacturer": ram.Manufacturer,
            "sn": ram.SerialNumber,
        }
        data.append(item_data)  # 将每条内存的信息,添加到一个列表里

    return  data  # 再对data列表封装一层,返回一个字典,方便上级方法的调用

# cpu 序列号
def get_CPU_info():
    cpu = []
    cp = s.Win32_Processor()
    for u in cp:
        cpu.append(
            {
                "Name": u.Name,
                "Serial Number": u.ProcessorId,
                "CoreNum": u.NumberOfCores
            }
        )
    #   print(":::CPU info:", json.dumps(cpu))
    return cpu


# 硬盘序列号
def get_disk_info():
    disk = []
    for pd in s.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))
    return disk


# mac 地址(包括虚拟机的)
def get_network_info():
    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))
    return network


# 主板序列号
def get_mainboard_info():
    mainboard = []
    for board_id in s.Win32_BaseBoard():
        mainboard.append(board_id.SerialNumber.strip().strip('.'))
    return mainboard

    #  由于机器码太长,故选取机器码字符串部分字符


#  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]

# gpu序列号
def get_gpu_info():
    #for video in s.Win32_VideoController():
    #    print("video: %s" % video)  # caption属性可以从1中得到

    gpuInfo=re.split('_|&',s.Win32_VideoController()[1].PNPDeviceID)
    return gpuInfo[1]+gpuInfo[3]+gpuInfo[5][4:8]+gpuInfo[5][0:4]


if __name__ == '__main__':
    print("get_disk_info Serial Number",get_disk_info()[0]["Serial"])
    print("get_CPU_info Serial Number",get_CPU_info()[0]['Serial Number'])
    print("get_network_info mac Number",get_network_info()[0]['MAC'])
    print("get_mainboard_info Serial Number",get_mainboard_info()[0])
    print("get_gpu_info Serial Number",get_gpu_info())
    print("get_memory_info Serial Number", get_memory_info())
    with open("test.txt", "w") as f:
        f.write("get_disk_info Serial Number "+str(get_disk_info()[0]["Serial"])+"\n")
        f.write("get_CPU_info Serial Number "+str(get_CPU_info()[0]['Serial Number'])+"\n")
        f.write("get_network_info mac Number "+str(get_network_info()[0]['MAC'])+"\n")
        f.write("get_mainboard_info Serial Number "+str(get_mainboard_info()[0])+"\n")
        f.write("get_gpu_info Serial Number "+str(get_gpu_info())+"\n")
        f.write("get_memory_info Serial Number "+ str(get_memory_info())+"\n")      
"C:\Program Files\Python36\python.exe" F:/sxj/untitled1/rename.py
get_disk_info Serial Number **AK****
get_CPU_info Serial Number **EBF*******906ED
get_network_info mac Number **:**:**:*0:00:01
get_mainboard_info Serial Number 1*****026302***
get_gpu_info Serial Number 10********770000      
F:\sxj\untitled1>pyinstaller -F collectcomputerInfo.py