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...
 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()

 


img_42a4adae4716d0e15c3eeaabfd040044.png

注:转载需注明出处及作者。

流柯      

目录
相关文章
|
3月前
|
人工智能 监控 安全
掌握Windows管理利器:WMI命令实战
本文介绍了Windows Management Instrumentation (WMI) 的基本概念和用途,通过多个实用的`wmic`命令示例,如获取CPU信息、查看操作系统详情、管理服务、检查磁盘空间等,展示了WMI在系统维护中的强大功能。适合IT专业人士学习和参考。
100 4
|
4月前
|
JavaScript 前端开发 API
MASM32编程通过WMI获取Windows计划任务
MASM32编程通过WMI获取Windows计划任务
|
4月前
|
Windows Python
python获取windows机子上运行的程序名称
python获取windows机子上运行的程序名称
|
4月前
|
Python Windows
python之windows脚本启动bat
python之windows脚本启动bat
|
3月前
|
C# 开发工具 Windows
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
93 0
|
4月前
|
安全 网络安全 API
基于WMI更新Windows系统信息采集程序sysInfo的一些收获
基于WMI更新Windows系统信息采集程序sysInfo的一些收获
|
5月前
|
Python Windows
【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?
【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?
|
8月前
|
开发者 索引 Python
实践:如何使用python在网页的表格里抓取信息
实践:如何使用python在网页的表格里抓取信息
132 0
|
SQL 关系型数据库 数据库连接
使用python3抓取pinpoint应用信息入库
使用python3通过pinpoint api来获取pinpoint中应用基础信息、上下游链路,并入库
1986 0
|
数据采集 供应链 机器人
Python - 抓取 iphone13 pro 线下店供货信息并发送到钉钉机器人,最后设置为定时任务
Python - 抓取 iphone13 pro 线下店供货信息并发送到钉钉机器人,最后设置为定时任务
459 0
Python - 抓取 iphone13 pro 线下店供货信息并发送到钉钉机器人,最后设置为定时任务