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 def cpu_use(): 28 #5s取一次CPU的使用率 29 c = wmi.WMI() 30 while True: 31 for cpu in c.Win32_Processor(): 32 timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime()) 33 print '%s | Utilization: %s: %d %%' % (timestamp, cpu.DeviceID, cpu.LoadPercentage) 34 time.sleep(5) 35 36 def disk(): 37 c = wmi.WMI () 38 #获取硬盘分区 39 for physical_disk in c.Win32_DiskDrive (): 40 for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"): 41 for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"): 42 print physical_disk.Caption.encode("UTF8"), partition.Caption.encode("UTF8"), logical_disk.Caption 43 44 #获取硬盘使用百分情况 45 for disk in c.Win32_LogicalDisk (DriveType=3): 46 print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size)) 47 48 def network(): 49 c = wmi.WMI () 50 #获取MAC和IP地址 51 for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1): 52 print "MAC: %s" % interface.MACAddress 53 for ip_address in interface.IPAddress: 54 print "ip_add: %s" % ip_address 55 print 56 57 #获取自启动程序的位置 58 for s in c.Win32_StartupCommand (): 59 print "[%s] %s <%s>" % (s.Location.encode("UTF8"), s.Caption.encode("UTF8"), s.Command.encode("UTF8")) 60 61 62 #获取当前运行的进程 63 for process in c.Win32_Process (): 64 print process.ProcessId, process.Name 65 66 def main(): 67 sys_version() 68 cpu_mem() 69 #disk() 70 #network() 71 #cpu_use() 72 73 if __name__ == '__main__': 74 main() 75 print platform.system() 76 print platform.release() 77 print platform.version() 78 print platform.platform() 79 print platform.machine()
注:转载需注明出处及作者。