当前位置:   article > 正文

基于python的电脑硬体设备信息获取_python获取cpu序列号

python获取cpu序列号

功能:

使用python编程实现电脑硬体信息的获取,主要包括电脑的CPU信息、主板信息、硬盘信息、内存信息、BIOS信息、电池信息等。

环境:

python==3.6.8      wmi==1.4.9

代码:

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os, sys
  4. import wmi
  5. c = wmi.WMI()
  6. #处理器
  7. def printCPU():
  8. tmpdict = {}
  9. tmpdict["CpuCores"] = 0
  10. for cpu in c.Win32_Processor():
  11. tmpdict["cpuid"] = cpu.ProcessorId.strip()
  12. tmpdict["CpuType"] = cpu.Name
  13. tmpdict['systemName'] = cpu.SystemName
  14. try:
  15. tmpdict["CpuCores"] = cpu.NumberOfCores
  16. except:
  17. tmpdict["CpuCores"] += 1
  18. tmpdict["CpuClock"] = cpu.MaxClockSpeed
  19. tmpdict['DataWidth'] = cpu.DataWidth
  20. print (tmpdict)
  21. return (tmpdict)
  22. #主板
  23. def printMain_board():
  24. boards = []
  25. # print len(c.Win32_BaseBoard()):
  26. for board_id in c.Win32_BaseBoard():
  27. tmpmsg = {}
  28. tmpmsg['UUID'] = board_id.qualifiers['UUID'][1:-1] #主板UUID,有的主板这部分信息取到为空值,ffffff-ffffff这样的
  29. tmpmsg['SerialNumber'] = board_id.SerialNumber #主板序列号
  30. tmpmsg['Manufacturer'] = board_id.Manufacturer #主板生产品牌厂家
  31. tmpmsg['Product'] = board_id.Product #主板型号
  32. boards.append(tmpmsg)
  33. print (boards)
  34. return (boards)
  35. #BIOS
  36. def printBIOS():
  37. bioss = []
  38. for bios_id in c.Win32_BIOS():
  39. tmpmsg = {}
  40. tmpmsg['BiosCharacteristics'] = bios_id.BiosCharacteristics #BIOS特征码
  41. tmpmsg['version'] = bios_id.Version #BIOS版本
  42. tmpmsg['Manufacturer'] = bios_id.Manufacturer.strip() #BIOS固件生产厂家
  43. tmpmsg['ReleaseDate'] = bios_id.ReleaseDate #BIOS释放日期
  44. tmpmsg['SMBIOSBIOSVersion'] = bios_id.SMBIOSBIOSVersion #系统管理规范版本
  45. bioss.append(tmpmsg)
  46. print (bioss)
  47. return (bioss)
  48. #硬盘
  49. def printDisk():
  50. disks = []
  51. for disk in c.Win32_DiskDrive():
  52. # print disk.__dict__
  53. tmpmsg = {}
  54. tmpmsg['SerialNumber'] = disk.SerialNumber.strip()
  55. tmpmsg['DeviceID'] = disk.DeviceID
  56. tmpmsg['Caption'] = disk.Caption
  57. tmpmsg['Size'] = disk.Size
  58. tmpmsg['UUID'] = disk.qualifiers['UUID'][1:-1]
  59. disks.append(tmpmsg)
  60. for d in disks:
  61. print (d)
  62. return (disks)
  63. #内存
  64. def printPhysicalMemory():
  65. memorys = []
  66. for mem in c.Win32_PhysicalMemory():
  67. tmpmsg = {}
  68. tmpmsg['UUID'] = mem.qualifiers['UUID'][1:-1]
  69. tmpmsg['BankLabel'] = mem.BankLabel
  70. tmpmsg['SerialNumber'] = mem.SerialNumber.strip()
  71. tmpmsg['ConfiguredClockSpeed'] = mem.ConfiguredClockSpeed
  72. tmpmsg['Capacity'] = mem.Capacity
  73. tmpmsg['ConfiguredVoltage'] = mem.ConfiguredVoltage
  74. memorys.append(tmpmsg)
  75. for m in memorys:
  76. print (m)
  77. return (memorys)
  78. #电池信息,只有笔记本才会有电池选项
  79. def printBattery():
  80. isBatterys = False
  81. for b in c.Win32_Battery():
  82. isBatterys = True
  83. return (isBatterys)
  84. #网卡mac地址:
  85. def printMacAddress():
  86. macs = []
  87. for n in c.Win32_NetworkAdapter():
  88. mactmp = n.MACAddress
  89. if mactmp and len(mactmp.strip()) > 5:
  90. tmpmsg = {}
  91. tmpmsg['MACAddress'] = n.MACAddress
  92. tmpmsg['Name'] = n.Name
  93. tmpmsg['DeviceID'] = n.DeviceID
  94. tmpmsg['AdapterType'] = n.AdapterType
  95. tmpmsg['Speed'] = n.Speed
  96. macs.append(tmpmsg)
  97. print (macs)
  98. return (macs)
  99. def main():
  100. printCPU()
  101. printMain_board()
  102. printBIOS()
  103. printDisk()
  104. printPhysicalMemory()
  105. printMacAddress()
  106. print (printBattery())
  107. if __name__ == '__main__':
  108. main()

结果:

适当的对上面代码改进,可以增加加密功能,并将密码写入到一个xml文件中。

  1. import os, sys
  2. import wmi
  3. import hashlib
  4. import base64
  5. c = wmi.WMI()
  6. #处理器
  7. def printCPU():
  8. tmpdict = {}
  9. tmpdict["CpuCores"] = 0
  10. for cpu in c.Win32_Processor():
  11. tmpdict["cpuid"] = cpu.ProcessorId.strip()
  12. tmpdict["CpuType"] = cpu.Name
  13. tmpdict['systemName'] = cpu.SystemName
  14. try:
  15. tmpdict["CpuCores"] = cpu.NumberOfCores
  16. except:
  17. tmpdict["CpuCores"] += 1
  18. tmpdict["CpuClock"] = cpu.MaxClockSpeed
  19. tmpdict['DataWidth'] = cpu.DataWidth
  20. print (tmpdict)
  21. return tmpdict
  22. #主板
  23. def printMain_board():
  24. boards = []
  25. # print len(c.Win32_BaseBoard()):
  26. for board_id in c.Win32_BaseBoard():
  27. tmpmsg = {}
  28. tmpmsg['UUID'] = board_id.qualifiers['UUID'][1:-1] #主板UUID,有的主板这部分信息取到为空值,ffffff-ffffff这样的
  29. tmpmsg['SerialNumber'] = board_id.SerialNumber #主板序列号
  30. tmpmsg['Manufacturer'] = board_id.Manufacturer #主板生产品牌厂家
  31. tmpmsg['Product'] = board_id.Product #主板型号
  32. boards.append(tmpmsg)
  33. print (boards)
  34. return boards
  35. #BIOS
  36. def printBIOS():
  37. bioss = []
  38. for bios_id in c.Win32_BIOS():
  39. tmpmsg = {}
  40. tmpmsg['BiosCharacteristics'] = bios_id.BiosCharacteristics #BIOS特征码
  41. tmpmsg['version'] = bios_id.Version #BIOS版本
  42. tmpmsg['Manufacturer'] = bios_id.Manufacturer.strip() #BIOS固件生产厂家
  43. tmpmsg['ReleaseDate'] = bios_id.ReleaseDate #BIOS释放日期
  44. tmpmsg['SMBIOSBIOSVersion'] = bios_id.SMBIOSBIOSVersion #系统管理规范版本
  45. bioss.append(tmpmsg)
  46. print (bioss)
  47. return bioss
  48. #硬盘
  49. def printDisk():
  50. disks = []
  51. for disk in c.Win32_DiskDrive():
  52. # print disk.__dict__
  53. tmpmsg = {}
  54. tmpmsg['SerialNumber'] = disk.SerialNumber.strip()
  55. tmpmsg['DeviceID'] = disk.DeviceID
  56. tmpmsg['Caption'] = disk.Caption
  57. tmpmsg['Size'] = disk.Size
  58. tmpmsg['UUID'] = disk.qualifiers['UUID'][1:-1]
  59. disks.append(tmpmsg)
  60. for d in disks:
  61. print (d)
  62. return disks
  63. #电池信息,只有笔记本才会有电池选项
  64. def printBattery():
  65. isBatterys = False
  66. for b in c.Win32_Battery():
  67. isBatterys = True
  68. return isBatterys
  69. #网卡mac地址:
  70. def printMacAddress():
  71. macs = []
  72. for n in c.Win32_NetworkAdapter():
  73. mactmp = n.MACAddress
  74. if mactmp and len(mactmp.strip()) > 5:
  75. tmpmsg = {}
  76. tmpmsg['MACAddress'] = n.MACAddress
  77. tmpmsg['Name'] = n.Name
  78. tmpmsg['DeviceID'] = n.DeviceID
  79. tmpmsg['AdapterType'] = n.AdapterType
  80. tmpmsg['Speed'] = n.Speed
  81. macs.append(tmpmsg)
  82. print (macs)
  83. return macs
  84. if __name__ == '__main__':
  85. hd1=printCPU()
  86. hd3=printMain_board()
  87. #printBIOS()
  88. #printDisk()
  89. hd2=printMacAddress()
  90. print(type(hd1))
  91. print(type(hd2))
  92. print(type(hd3))
  93. print(hd1['cpuid'],hd1['systemName'],hd2[0]['MACAddress'],hd3[0]['UUID'],hd3[0]['SerialNumber'])
  94. sum = hd1['cpuid'] + hd1['systemName'] + hd2[0]['MACAddress'] + hd3[0]['UUID']+ hd3[0]['SerialNumber']
  95. shastr = sum.encode('gbk')
  96. sha = hashlib.sha1(shastr).hexdigest()
  97. if (sha == 'b696fcbb46e27a09458019942dd10856b0023fea'):
  98. print('reg user')
  99. else:
  100. print('no reg user')
  101. print(sha)
  102. #print(sum)
  103. #print(type(sum))
  104. s1 = base64.encodestring(shastr)
  105. print(s1)
  106. #s2 = base64.decodestring(s1)
  107. #print(s1,s2)
  108. #print (printBattery())
  109. xmlfilename = 'result.xml'
  110. f = open(xmlfilename, 'w') # 若是'wb'就表示写二进制文件
  111. f.write(sha)
  112. f.close()

结果:

xml的结果: 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/156758
推荐阅读
相关标签
  

闽ICP备14008679号