This article describes how to use WMI to detect windows system information, hard disk information, and nic information in python, and describes how to perform operations on system information in Python, for more information about How to Use WMI to detect windows system information, hard disk information, and nic information. Share it with you for your reference. The specific implementation method is as follows:
#! /Usr/bin/env python #-*-coding: UTF-8-*-import wmi import sys, time, platform def get_system_info (OS): "Get the OS version. "Print" Operating system: "if OS =" Windows ": c = wmi. WMI () for sys in c. win32_OperatingSystem (): print '\ t' + "Version: \ t % s" % sys. caption. encode ("GBK") print '\ t' + "Vernum: \ t % s" % sys. buildNumber def get_memory_info (OS): "Get physical memory and virtual memory. "Print" memory_info: "if OS =" Windows ": c = wmi. WMI () cs = c. win32_ComputerSystem () pfu = c. win32_PageFileUsage () MemTotal = int (cs [0]. totalPhysicalMemory)/1024/1024 print '\ t' + "TotalPhysicalMemory:" +' \ t' + str (MemTotal) + "M" # tmpdict ["MemFree"] = int (OS [0]. freePhysicalMemory)/1024 SwapTotal = int (pfu [0]. allocatedBaseSize) print '\ t' + "SwapTotal:" +' \ t' + str (SwapTotal) + "M" # Tmpdict ["SwapFree"] = int (pfu [0]. AllocatedBaseSize-pfu [0]. CurrentUsage) def get_disk_info (OS): "Get physical disk information. "Print" disk_info: "if OS =" Windows ": tmplist = [] c = wmi. WMI () for physical_disk in c. win32_DiskDrive (): if physical_disk.Size: print '\ t' + str (physical_disk.Caption) +': \ t' + str (long (physical_disk.Size)/1024/1024/1024) + "G" def get_cpu_info (OS): "to obtain CPU information. "Print" cpu_info: "if OS =" Windows ": tmpdict = {} tmpdict [" CpuCores "] = 0 c = wmi. WMI () for cpu in c. win32_Processor (): tmpdict ["CpuType"] = cpu. name try: tmpdict ["CpuCores"] = cpu. numberOfCores failed T: tmpdict ["CpuCores"] + = 1 tmpdict ["CpuClock"] = cpu. maxClockSpeed print '\ t' + 'cputype: \ t' + str (tmpdict ["CpuType"]) print' \ t' + 'cpucores: \ t' + str (tmpdict ["CpuCores"]) def get_netw Ork_info (OS): "to obtain the NIC information and current TCP connections. "Print" network_info: "if OS =" Windows ": tmplist = [] c = wmi. WMI () for interface in c. win32_NetworkAdapterConfiguration (IPEnabled = 1): tmpdict ={} tmpdict ["Description"] = interface. description tmpdict ["IPAddress"] = interface. IPAddress [0] tmpdict ["IPSubnet"] = interface. IPSubnet [0] tmpdict ["MAC"] = interface. MACAddress tmplist. append (tmpdict) for I in tmplist: print '\ t' + I ["Description"] print' \ t' + "MAC: "+ '\ t' + I [" MAC "] print' \ t' +" IPAddress: "+ '\ t' + I [" IPAddress "] print' \ t' +" IPSubnet: "+ '\ t' + I [" IPSubnet "] for interfacePerfTCP in c. win32_PerfRawData_Tcpip_TCPv4 (): print '\ t' + 'tcp Connect: \ t' + str (interfacePerfTCP. connectionsEstablished) if _ name _ = "_ main _": OS = platform. system () get_system_info (OS) get_memory_info (OS) get_disk_info (OS) get_cpu_info (OS) get_network_info (OS)
I hope this article will help you with Python programming.