Python uses the WMI module to obtain the hardware information of the windowns system: hard disk partition, usage, memory size, CPU model, current running process, self-starting program and location, and system version.
- #! /Usr/bin/envpython
- #-*-Coding: UTF-8 -*-
- Importwmi
- Importos
- Importsys
- Importplatform
- Importtime
- Defsys_version ():
- C = wmi. WMI ()
- # Obtain the operating system version
- Forsysinc. Win32_OperatingSystem ():
- Print "Version: % s" % sys. Caption. encode ("UTF8"), "Vernum: % s" % sys. BuildNumber
- Printsys. OSArchitecture. encode ("UTF8") # whether the system is 32-bit or 64-bit
- Printsys. NumberOfProcesses # Total number of processes running on the current system
- Defcpu_mem ():
- C = wmi. WMI ()
- # CPU type and memory
- Forprocessorinc. Win32_Processor ():
- # Print "ProcessorID: % s" % processor. DeviceID
- Print "ProcessName: % s" % processor. Name. strip ()
- ForMemoryinc. Win32_PhysicalMemory ():
- Print "MemoryCapacity: %. fMB" % (int (Memory. Capacity)/1048576)
- Defcpu_use ():
- # CPU usage per 5 seconds
- C = wmi. WMI ()
- WhileTrue:
- Forcpuinc. Win32_Processor ():
- Timestamp = time. strftime ('% a, % d % B % Y % H: % M: % s', time. localtime ())
- Print '% s | Utilization: % s: % d %' % (timestamp, cpu. DeviceID, cpu. LoadPercentage)
- Time. sleep (5)
- Defdisk ():
- C = wmi. WMI ()
- # Obtain Hard Disk Partitions
- Forphysical_diskinc.Win32_DiskDrive ():
- Forpartitioninphysical_disk.associators ("Win32_DiskDriveToDiskPartition "):
- Forlogical_diskinpartition.associators ("Win32_LogicalDiskToPartition "):
- Printphysical_disk.Caption.encode ("UTF8"), partition. Caption. encode ("UTF8"), logical_disk.Caption
- # Obtain the disk usage percentage
- Fordiskinc. Win32_LogicalDisk (DriveType = 3 ):
- Printdisk. Caption, "% 0.2f % free" % (100.0 * long (disk. FreeSpace)/long (disk. Size ))
- Defnetwork ():
- C = wmi. WMI ()
- # Obtain MAC and IP addresses
- Forinterfaceinc. Win32_NetworkAdapterConfiguration (IPEnabled = 1 ):
- Print "MAC: % s" % interface. MACAddress
- Forip_addressininterface.IPAddress:
- Print "ip_add: % s" % ip_address
- Print
- # Obtain the position of the self-starting Program
- Forsinc. Win32_StartupCommand ():
- Print "[% s] % s <% s>" % (s. location. encode ("UTF8"), s. caption. encode ("UTF8"), s. command. encode ("UTF8 "))
- # Obtain the currently running process
- Forprocessinc. Win32_Process ():
- Printprocess. ProcessId, process. Name
- Defmain ():
- Sys_version ()
- # Cpu_mem ()
- # Disk ()
- # Network ()
- # Cpu_use ()
- If _ name __= = '_ main __':
- Main ()
- Printplatform. system ()
- Printplatform. release ()
- Printplatform. version ()
- Printplatform. platform ()
- Printplatform. machine ()