Python用WMI模块获取windowns系统的硬件信息:硬盘分区、使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息。
-
-
-
- import wmi
- import os
- import sys
- import platform
- import time
-
- def sys_version():
- c = wmi.WMI ()
-
- for sys in c.Win32_OperatingSystem():
- print "Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber
- print sys.OSArchitecture.encode("UTF8")
- print sys.NumberOfProcesses #当前系统运行的进程总数
-
- def cpu_mem():
- c = wmi.WMI ()
-
- for processor in c.Win32_Processor():
-
- print "Process Name: %s" % processor.Name.strip()
- for Memory in c.Win32_PhysicalMemory():
- print "Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576)
-
- def cpu_use():
-
- c = wmi.WMI()
- while True:
- for cpu in c.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)
-
- def disk():
- c = wmi.WMI ()
-
- for physical_disk in c.Win32_DiskDrive ():
- for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):
- for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):
- print physical_disk.Caption.encode("UTF8"), partition.Caption.encode("UTF8"), logical_disk.Caption
-
-
- for disk in c.Win32_LogicalDisk (DriveType=3):
- print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size))
-
- def network():
- c = wmi.WMI ()
-
- for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
- print "MAC: %s" % interface.MACAddress
- for ip_address in interface.IPAddress:
- print "ip_add: %s" % ip_address
- print
-
-
- for s in c.Win32_StartupCommand ():
- print "[%s] %s <%s>" % (s.Location.encode("UTF8"), s.Caption.encode("UTF8"), s.Command.encode("UTF8"))
-
-
-
- for process in c.Win32_Process ():
- print process.ProcessId, process.Name
-
- def main():
- sys_version()
-
-
-
-
-
- if __name__ == '__main__':
- main()
- print platform.system()
- print platform.release()
- print platform.version()
- print platform.platform()
- print platform.machine()
本文转自 lover00751CTO博客,原文链接:http://blog.51cto.com/wangwei007/1158075,如需转载请自行联系原作者