参考文档:https://www.cnblogs.com/aguncn/archive/2013/09/03/3298583.html
说明
本文主要引用两个库来完成对Windows基本信息的读取,读取信息包括CPU、磁盘、IP地址、安装的程序等。
wmi
官方文档:https://pypi.python.org/pypi/WMI/
- 由于无法直接用pip进行安装,需要将下载过来的包解压后,在解压文件setup.py目录下运行
python setup.py install
- 注意需要的依赖包
pywin32
,此处由于程序运行报错才根据其他包的安装解决,参考:https://stackoverflow.com/questions/33471804/no-module-named-win32com-client
程序
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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,"Vernum:%s" % sys.BuildNumber
print sys.OSArchitecture#系统是32位还是64位的
print sys.NumberOfProcesses #当前系统运行的进程总数
def cpu_mem():
c = wmi.WMI ()
#CPU类型和内存
for processor in c.Win32_Processor():
#print "Processor ID: %s" % processor.DeviceID
print "Process Name: %s" % processor.Name.strip()
for Memory in c.Win32_PhysicalMemory():
print "Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576)
def cpu_use():
#5s取一次CPU的使用率
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)
break
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, partition.Caption, 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 ()
#获取MAC和IP地址
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, s.Caption, s.Command)
#获取当前运行的进程
for process in c.Win32_Process ():
print process.ProcessId, process.Name
def main():
sys_version()
cpu_mem()
disk()
network()
#cpu_use()
if __name__ == '__main__':
main()
print platform.system()
print platform.release()
print platform.version()
print platform.platform()
print platform.machine()