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

本文实例讲述了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程序设计有所帮助。

Posted in 未分类

发表评论