python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法


Posted in Python onMay 15, 2015

本文实例讲述了python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
import wmi 
import sys,time,platform 
def get_system_info(os): 
  """ 
  获取操作系统版本。 
  """ 
  print 
  print "Operating system:" 
  if os == "Windows": 
    c = wmi.WMI () 
    for sys in c.Win32_OperatingSystem(): 
      print '\t' + "Version :\t%s" % sys.Caption.encode("GBK") 
      print '\t' + "Vernum :\t%s" % sys.BuildNumber 
def get_memory_info(os): 
  """ 
  获取物理内存和虚拟内存。 
  """ 
  print 
  print "memory_info:" 
  if os == "Windows": 
    c = wmi.WMI () 
    cs = c.Win32_ComputerSystem() 
    pfu = c.Win32_PageFileUsage() 
    MemTotal = int(cs[0].TotalPhysicalMemory)/1024/1024 
    print '\t' + "TotalPhysicalMemory :" + '\t' + str(MemTotal) + "M" 
    #tmpdict["MemFree"] = int(os[0].FreePhysicalMemory)/1024 
    SwapTotal = int(pfu[0].AllocatedBaseSize) 
    print '\t' + "SwapTotal :" + '\t' + str(SwapTotal) + "M" 
    #tmpdict["SwapFree"] = int(pfu[0].AllocatedBaseSize - pfu[0].CurrentUsage) 
def get_disk_info(os): 
  """ 
  获取物理磁盘信息。 
  """ 
  print 
  print "disk_info:" 
  if os == "Windows": 
    tmplist = [] 
    c = wmi.WMI () 
    for physical_disk in c.Win32_DiskDrive(): 
      if physical_disk.Size: 
        print '\t' + str(physical_disk.Caption) + ' :\t' + str(long(physical_disk.Size)/1024/1024/1024) + "G" 
def get_cpu_info(os): 
  """ 
  获取CPU信息。 
  """ 
  print 
  print "cpu_info:" 
  if os == "Windows": 
    tmpdict = {} 
    tmpdict["CpuCores"] = 0 
    c = wmi.WMI () 
    for cpu in c.Win32_Processor():       
      tmpdict["CpuType"] = cpu.Name 
    try: 
      tmpdict["CpuCores"] = cpu.NumberOfCores 
    except: 
      tmpdict["CpuCores"] += 1 
      tmpdict["CpuClock"] = cpu.MaxClockSpeed   
    print '\t' + 'CpuType :\t' + str(tmpdict["CpuType"]) 
    print '\t' + 'CpuCores :\t' + str(tmpdict["CpuCores"]) 
def get_network_info(os): 
  """ 
  获取网卡信息和当前TCP连接数。 
  """ 
  print 
  print "network_info:" 
  if os == "Windows": 
    tmplist = [] 
    c = wmi.WMI () 
    for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1): 
        tmpdict = {} 
        tmpdict["Description"] = interface.Description 
        tmpdict["IPAddress"] = interface.IPAddress[0] 
        tmpdict["IPSubnet"] = interface.IPSubnet[0] 
        tmpdict["MAC"] = interface.MACAddress 
        tmplist.append(tmpdict) 
    for i in tmplist: 
      print '\t' + i["Description"] 
      print '\t' + '\t' + "MAC :" + '\t' + i["MAC"] 
      print '\t' + '\t' + "IPAddress :" + '\t' + i["IPAddress"] 
      print '\t' + '\t' + "IPSubnet :" + '\t' + i["IPSubnet"] 
    for interfacePerfTCP in c.Win32_PerfRawData_Tcpip_TCPv4(): 
        print '\t' + 'TCP Connect :\t' + str(interfacePerfTCP.ConnectionsEstablished) 
if __name__ == "__main__": 
  os = platform.system() 
  get_system_info(os) 
  get_memory_info(os) 
  get_disk_info(os) 
  get_cpu_info(os) 
  get_network_info(os)

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python 文件读写操作实例详解
Mar 12 Python
Python操作MySQL简单实现方法
Jan 26 Python
Python利用前序和中序遍历结果重建二叉树的方法
Apr 27 Python
Python中列表和元组的使用方法和区别详解
Dec 30 Python
基于hashlib模块--加密(详解)
Jun 21 Python
Python 字符串与二进制串的相互转换示例
Jul 23 Python
对Python中的条件判断、循环以及循环的终止方法详解
Feb 08 Python
简单了解django orm中介模型
Jul 30 Python
Python列表原理与用法详解【创建、元素增加、删除、访问、计数、切片、遍历等】
Oct 30 Python
Python基于模块Paramiko实现SSHv2协议
Apr 28 Python
Python Opencv实现单目标检测的示例代码
Sep 08 Python
pytorch 如何使用float64训练
May 24 Python
python获取当前时间对应unix时间戳的方法
May 15 #Python
Python加pyGame实现的简单拼图游戏实例
May 15 #Python
Python实现从URL地址提取文件名的方法
May 15 #Python
Python基础入门之seed()方法的使用
May 15 #Python
Python中的random()方法的使用介绍
May 15 #Python
Python的randrange()方法使用教程
May 15 #Python
Python中的choice()方法使用详解
May 15 #Python
You might like
用PHP 4.2书写安全的脚本
2006/10/09 PHP
php数组函数序列之array_search()- 按元素值返回键名
2011/11/04 PHP
浅谈discuz密码加密的方式
2014/05/22 PHP
Smarty中常用变量操作符汇总
2014/10/27 PHP
33道php常见面试题及答案
2015/07/06 PHP
PHP设计模式之建造者模式定义与用法简单示例
2018/08/13 PHP
对javascript的一点点认识总结《javascript高级程序设计》读书笔记
2011/11/30 Javascript
JavaScript实现向setTimeout执行代码传递参数的方法
2015/04/16 Javascript
在AngularJS框架中处理数据建模的方式解析
2016/03/05 Javascript
jQuery简易时光轴实现方法示例
2017/03/13 Javascript
浅谈nodejs中的类定义和继承的套路
2017/07/26 NodeJs
Element-ui table中过滤条件变更表格内容的方法
2018/03/02 Javascript
React Router V4使用指南(精讲)
2018/09/17 Javascript
基于layui轮播图满屏是高度自适应的解决方法
2019/09/16 Javascript
js+canvas实现两张图片合并成一张图片的方法
2019/11/01 Javascript
[02:12]DOTA2英雄基础教程 变体精灵
2013/12/16 DOTA
Python使用回溯法子集树模板解决爬楼梯问题示例
2017/09/08 Python
python爬虫面试宝典(常见问题)
2018/03/02 Python
Python实现购物车程序
2018/04/16 Python
将Python字符串生成PDF的实例代码详解
2019/05/17 Python
Python 常用模块 re 使用方法详解
2019/06/06 Python
python实现大量图片重命名
2020/03/23 Python
matplotlib实现显示伪彩色图像及色度条
2019/12/07 Python
pytorch实现mnist数据集的图像可视化及保存
2020/01/14 Python
利用Python计算KS的实例详解
2020/03/03 Python
keras输出预测值和真实值方式
2020/06/27 Python
django restframework serializer 增加自定义字段操作
2020/07/15 Python
图片上传插件ImgUploadJS:用HTML5 File API 实现截图粘贴上传、拖拽上传
2016/01/20 HTML / CSS
社会实践自我鉴定
2013/11/07 职场文书
工厂保洁员岗位职责
2013/12/04 职场文书
冰淇淋店创业计划书范文
2013/12/27 职场文书
初中校园广播稿
2014/02/02 职场文书
艺人经纪人岗位职责
2014/04/15 职场文书
带刀到教室的检讨书
2014/10/04 职场文书
《扇形统计图》教学反思
2016/02/17 职场文书
pytorch 中autograd.grad()函数的用法说明
2021/05/12 Python