python写的用WMI检测windows系统信息的脚本

简介:

 闲来无聊,用python写了一个检测windows系统硬件信息的脚本,主要就是用WMI模块来完成,分享给大家,希望对大家有所帮助。linux的系统信息直接用shell即可获取,这里不做介绍。获取主要检测内容为:系统平台,内存,硬盘硬件信息(详细的之前写了一个,大家参考),CPU信息,网卡信息等。

 
  1. #!/usr/bin/env python 
  2. # -*- coding: utf-8 -*- 
  3. import wmi 
  4. import sys,time,platform 
  5.  
  6. def get_system_info(os): 
  7.     """  
  8.     获取操作系统版本。  
  9.     """  
  10.     print 
  11.     print "Operating system:" 
  12.     if os == "Windows"
  13.         c = wmi.WMI () 
  14.         for sys in c.Win32_OperatingSystem(): 
  15.             print '\t' + "Version :\t%s" % sys.Caption.encode("GBK"
  16.             print '\t' + "Vernum :\t%s" % sys.BuildNumber 
  17.  
  18. def get_memory_info(os): 
  19.     """  
  20.     获取物理内存和虚拟内存。  
  21.     """  
  22.     print 
  23.     print "memory_info:" 
  24.     if os == "Windows"
  25.         c = wmi.WMI () 
  26.         cs = c.Win32_ComputerSystem()  
  27.         pfu = c.Win32_PageFileUsage()  
  28.         MemTotal = int(cs[0].TotalPhysicalMemory)/1024/1024 
  29.         print '\t' + "TotalPhysicalMemory :" + '\t' + str(MemTotal) + "M" 
  30.         #tmpdict["MemFree"] = int(os[0].FreePhysicalMemory)/1024  
  31.         SwapTotal = int(pfu[0].AllocatedBaseSize) 
  32.         print '\t' + "SwapTotal :" + '\t' + str(SwapTotal) + "M" 
  33.         #tmpdict["SwapFree"] = int(pfu[0].AllocatedBaseSize - pfu[0].CurrentUsage) 
  34.  
  35. def get_disk_info(os):  
  36.     """  
  37.     获取物理磁盘信息。  
  38.     """  
  39.     print 
  40.     print "disk_info:" 
  41.     if os == "Windows"
  42.         tmplist = []  
  43.         c = wmi.WMI () 
  44.         for physical_disk in c.Win32_DiskDrive(): 
  45.             if physical_disk.Size: 
  46.                 print '\t' + str(physical_disk.Caption) + ' :\t' + str(long(physical_disk.Size)/1024/1024/1024) + "G" 
  47.  
  48. def get_cpu_info(os):  
  49.     """  
  50.     获取CPU信息。  
  51.     """  
  52.     print 
  53.     print "cpu_info:" 
  54.     if os == "Windows"
  55.         tmpdict = {}  
  56.         tmpdict["CpuCores"] = 0  
  57.         c = wmi.WMI ()  
  58.         for cpu in c.Win32_Processor():             
  59.             tmpdict["CpuType"] = cpu.Name  
  60.         try:  
  61.             tmpdict["CpuCores"] = cpu.NumberOfCores  
  62.         except:  
  63.             tmpdict["CpuCores"] += 1  
  64.             tmpdict["CpuClock"] = cpu.MaxClockSpeed     
  65.         print '\t' + 'CpuType :\t' + str(tmpdict["CpuType"]) 
  66.         print '\t' + 'CpuCores :\t' + str(tmpdict["CpuCores"]) 
  67.  
  68.  
  69. def get_network_info(os):  
  70.     """  
  71.     获取网卡信息和当前TCP连接数。  
  72.     """  
  73.     print 
  74.     print "network_info:" 
  75.     if os == "Windows"
  76.         tmplist = []  
  77.         c = wmi.WMI ()  
  78.         for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):  
  79.                 tmpdict = {}  
  80.                 tmpdict["Description"] = interface.Description  
  81.                 tmpdict["IPAddress"] = interface.IPAddress[0]  
  82.                 tmpdict["IPSubnet"] = interface.IPSubnet[0]  
  83.                 tmpdict["MAC"] = interface.MACAddress 
  84.                 tmplist.append(tmpdict)  
  85.         for i in tmplist: 
  86.             print '\t' + i["Description"
  87.             print '\t' + '\t' + "MAC :" + '\t' + i["MAC"
  88.             print '\t' + '\t' + "IPAddress :" + '\t' + i["IPAddress"
  89.             print '\t' + '\t' + "IPSubnet :" + '\t' + i["IPSubnet"
  90.         for interfacePerfTCP in c.Win32_PerfRawData_Tcpip_TCPv4():  
  91.                 print '\t' + 'TCP Connect :\t' + str(interfacePerfTCP.ConnectionsEstablished)  
  92.  
  93. if __name__ == "__main__"
  94.     os = platform.system() 
  95.     get_system_info(os) 
  96.     get_memory_info(os) 
  97.     get_disk_info(os) 
  98.     get_cpu_info(os) 
  99.     get_network_info(os) 


运行结果如下:

 

 


本文转自 lover00751CTO博客,原文链接:http://blog.51cto.com/wangwei007/1033760,如需转载请自行联系原作者


相关文章
|
2月前
|
Linux Shell Python
Linux执行Python脚本
Linux执行Python脚本
30 1
|
28天前
|
人工智能 机器人 C++
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
|
5天前
|
机器学习/深度学习 算法 数据可视化
Python用KNN(K-近邻)回归、分类、异常值检测预测房价、最优K值选取、误差评估可视化
Python用KNN(K-近邻)回归、分类、异常值检测预测房价、最优K值选取、误差评估可视化
|
7天前
|
数据采集 关系型数据库 BI
Python路面平整度检测车辆数据——速度修正
Python路面平整度检测车辆数据——速度修正
14 0
|
12天前
|
5G Python
Windows11搭建Python环境(Anaconda安装与使用)
Windows11搭建Python环境(Anaconda安装与使用)
35 0
|
17天前
|
数据采集 存储 安全
python检测代理ip是否可用的方法
python检测代理ip是否可用的方法
|
18天前
|
数据可视化 Python
python对网络图networkx进行社区检测和彩色绘图
python对网络图networkx进行社区检测和彩色绘图
|
19天前
|
算法 数据可视化 数据挖掘
用R语言和python进行社交网络中的社区检测
用R语言和python进行社交网络中的社区检测https://ucc.alicdn.com/pic/developer-ecology/4as3qn2go3ure_f4b58eedb2f849418cedab74ed3c856a.png
19 0
|
23天前
|
Windows
【Windows】 手写脚本更快编辑hosts文件
【Windows】 手写脚本更快编辑hosts文件
21 0
|
26天前
|
JSON 测试技术 持续交付
自动化测试与脚本编写:Python实践指南
【4月更文挑战第9天】本文探讨了Python在自动化测试中的应用,强调其作为热门选择的原因。Python拥有丰富的测试框架(如unittest、pytest、nose)以支持自动化测试,简化测试用例的编写与维护。示例展示了使用unittest进行单元测试的基本步骤。此外,Python还适用于集成测试、系统测试等,提供模拟外部系统行为的工具。在脚本编写实践中,Python的灵活语法和强大库(如os、shutil、sqlite3、json)助力执行复杂测试任务。同时,Python支持并发、分布式执行及与Jenkins、Travis CI等持续集成工具的集成,提升测试效率和质量。