当前位置:Gxlcms > Python > python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法

python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法

时间:2021-07-01 10:21:17 帮助过:98人阅读

本文实例讲述了python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法。分享给大家供大家参考。具体实现方法如下:

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import wmi
  4. import sys,time,platform
  5. def get_system_info(os):
  6. """
  7. 获取操作系统版本。
  8. """
  9. print
  10. print "Operating system:"
  11. if os == "Windows":
  12. c = wmi.WMI ()
  13. for sys in c.Win32_OperatingSystem():
  14. print '\t' + "Version :\t%s" % sys.Caption.encode("GBK")
  15. print '\t' + "Vernum :\t%s" % sys.BuildNumber
  16. def get_memory_info(os):
  17. """
  18. 获取物理内存和虚拟内存。
  19. """
  20. print
  21. print "memory_info:"
  22. if os == "Windows":
  23. c = wmi.WMI ()
  24. cs = c.Win32_ComputerSystem()
  25. pfu = c.Win32_PageFileUsage()
  26. MemTotal = int(cs[0].TotalPhysicalMemory)/1024/1024
  27. print '\t' + "TotalPhysicalMemory :" + '\t' + str(MemTotal) + "M"
  28. #tmpdict["MemFree"] = int(os[0].FreePhysicalMemory)/1024
  29. SwapTotal = int(pfu[0].AllocatedBaseSize)
  30. print '\t' + "SwapTotal :" + '\t' + str(SwapTotal) + "M"
  31. #tmpdict["SwapFree"] = int(pfu[0].AllocatedBaseSize - pfu[0].CurrentUsage)
  32. def get_disk_info(os):
  33. """
  34. 获取物理磁盘信息。
  35. """
  36. print
  37. print "disk_info:"
  38. if os == "Windows":
  39. tmplist = []
  40. c = wmi.WMI ()
  41. for physical_disk in c.Win32_DiskDrive():
  42. if physical_disk.Size:
  43. print '\t' + str(physical_disk.Caption) + ' :\t' + str(long(physical_disk.Size)/1024/1024/1024) + "G"
  44. def get_cpu_info(os):
  45. """
  46. 获取CPU信息。
  47. """
  48. print
  49. print "cpu_info:"
  50. if os == "Windows":
  51. tmpdict = {}
  52. tmpdict["CpuCores"] = 0
  53. c = wmi.WMI ()
  54. for cpu in c.Win32_Processor():
  55. tmpdict["CpuType"] = cpu.Name
  56. try:
  57. tmpdict["CpuCores"] = cpu.NumberOfCores
  58. except:
  59. tmpdict["CpuCores"] += 1
  60. tmpdict["CpuClock"] = cpu.MaxClockSpeed
  61. print '\t' + 'CpuType :\t' + str(tmpdict["CpuType"])
  62. print '\t' + 'CpuCores :\t' + str(tmpdict["CpuCores"])
  63. def get_network_info(os):
  64. """
  65. 获取网卡信息和当前TCP连接数。
  66. """
  67. print
  68. print "network_info:"
  69. if os == "Windows":
  70. tmplist = []
  71. c = wmi.WMI ()
  72. for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
  73. tmpdict = {}
  74. tmpdict["Description"] = interface.Description
  75. tmpdict["IPAddress"] = interface.IPAddress[0]
  76. tmpdict["IPSubnet"] = interface.IPSubnet[0]
  77. tmpdict["MAC"] = interface.MACAddress
  78. tmplist.append(tmpdict)
  79. for i in tmplist:
  80. print '\t' + i["Description"]
  81. print '\t' + '\t' + "MAC :" + '\t' + i["MAC"]
  82. print '\t' + '\t' + "IPAddress :" + '\t' + i["IPAddress"]
  83. print '\t' + '\t' + "IPSubnet :" + '\t' + i["IPSubnet"]
  84. for interfacePerfTCP in c.Win32_PerfRawData_Tcpip_TCPv4():
  85. print '\t' + 'TCP Connect :\t' + str(interfacePerfTCP.ConnectionsEstablished)
  86. if __name__ == "__main__":
  87. os = platform.system()
  88. get_system_info(os)
  89. get_memory_info(os)
  90. get_disk_info(os)
  91. get_cpu_info(os)
  92. get_network_info(os)

希望本文所述对大家的Python程序设计有所帮助。

人气教程排行