Python通过WMI模块获取Windows系统信息,对系统参数进行监控。
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 #http://www.cnblogs.com/liu-ke/ 4 import wmi 5 import os 6 import sys 7 import platform 8 import time 9 10 def sys_version(): 11 c = wmi.WMI () 12 #获取操作系统版本 13 for sys in c.Win32_OperatingSystem(): 14 print "Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber 15 print sys.OSArchitecture.encode("UTF8")#系统是32位还是64位的 16 print sys.NumberOfProcesses #当前系统运行的进程总数17 18 def cpu_mem(): 19 c = wmi.WMI () 20 #CPU类型和内存 21 for processor in c.Win32_Processor(): 22 #print "Processor ID: %s" % processor.DeviceID 23 print "Process Name: %s" % processor.Name.strip() 24 for Memory in c.Win32_PhysicalMemory(): 25 print "Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576) 26 27 28 29 def disk(): 30 c = wmi.WMI () 31 #获取硬盘分区 32 for physical_disk in c.Win32_DiskDrive (): 33 for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"): 34 for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"): 35 print physical_disk.Caption.encode("UTF8"), partition.Caption.encode("UTF8"), logical_disk.Caption 36 37 #获取硬盘使用百分情况 38 for disk in c.Win32_LogicalDisk (DriveType=3): 39 print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size)) 40 41 def network(): 42 c = wmi.WMI () 43 #获取MAC和IP地址 44 for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1): 45 print "MAC: %s" % interface.MACAddress 46 for ip_address in interface.IPAddress: 47 print "ip_add: %s" % ip_address 48 print 49 50 51 def main(): 52 sys_version() 53 cpu_mem() 54 #disk() 55 #network() 56 57 if __name__ == '__main__': 58 main() 59 print platform.system() 60 print platform.release() 61 print platform.version() 62 print platform.platform() 63 print platform.machine()