python wmi模块 获取windows内部信息
WMI (Windows Management Instrumentation) 模块可用于获取 Windows 内部信息,在使用Python获取Windows系统上的相关的信息可以使用WMI接口来获取,
什么是wmi?
WMI是一项核心的Windows管理技术,WMI作为一种规范和基础结构,通过它可以访问、配置、管理和监视几乎所有的Windows资源,比如用户可以在远程计算机器上启动一个进程;设定一个在特定日期和时间运行的进程;远程启动计算机;获得本地或远程计算机的已安装程序列表;查询本地或远程计算机的Windows事件日志等等。
该模块需要 win32com 的支持,环境安装如下,
pip install wmi
pip install pypiwin32
测试安装结果
>>> import win32con
>>> import wmi
导入模块没有报错,证明安装完成
wmi.WMI() 用于生成wmi实例
>>> wmi.WMI()
<_wmi_namespace: <COMObject winmgmts:>>
查询CPU信息
用于获取CPU处理器信息对象,并存以列表形式
wmi.Win32_Processor() 方法
返回参数: processorList
processorList: list类型,list中每个元素均为一个含cpu信息的object对象
import wmi w = wmi.WMI()
cpu_list = w.Win32_Processor()
print(cpu_list)
# [<_wmi_object: b'\\\\QH-20181120YSCF\\root\\cimv2:Win32_Processor.DeviceID="CPU0"'>]
import wmi w = wmi.WMI()
cpu_list = w.Win32_Processor() for cpu in cpu_list:
print(cpu)
instance of Win32_Processor
{
AddressWidth = 64;
Architecture = 9;
Availability = 3;
Caption = "Intel64 Family 6 Model 69 Stepping 1";
CpuStatus = 1;
CreationClassName = "Win32_Processor";
CurrentClockSpeed = 1587;
CurrentVoltage = 7;
DataWidth = 64;
Description = "Intel64 Family 6 Model 69 Stepping 1";
DeviceID = "CPU0";
ExtClock = 100;
Family = 205;
L2CacheSize = 256;
L3CacheSize = 3072;
L3CacheSpeed = 0;
Level = 6;
LoadPercentage = 32;
Manufacturer = "GenuineIntel";
MaxClockSpeed = 2301;
Name = "Intel(R) Core(TM) i5-4200U CPU @ 1.60GHz";
NumberOfCores = 2;
NumberOfLogicalProcessors = 4;
PowerManagementSupported = FALSE;
ProcessorId = "BFEBFBFF00040651";
ProcessorType = 3;
Revision = 17665;
Role = "CPU";
SocketDesignation = "CPU Socket - U3E1";
Status = "OK";
StatusInfo = 3;
SystemCreationClassName = "Win32_ComputerSystem";
SystemName = "QH-20181120YSCF";
UpgradeMethod = 33;
Version = "";
};
cpu.Name 是cpu的型号
cpu.NumberOfCores 是cpu的核心数
import wmi w = wmi.WMI()
cpu_list = w.Win32_Processor() for cpu in cpu_list:
print("cpu核心数",cpu.NumberOfCores)
print("cpu型号",cpu.Name) ''''
cpu核心数 2
cpu型号 Intel(R) Core(TM) i5-4200U CPU @ 1.60GHz
'''
len(cpu_list) 统计列表里面cpu个数 cpu_count
import wmi w = wmi.WMI()
cpu_list = w.Win32_Processor() # 统计列表cpu个数
print(len(cpu_list))
# 一个CPU
#
wmi.Win32_ComputerSystem() 方法
通过Win32_ComputerSystem()方法 可以获取机器的 制造商Manufacturer , 机器型号
import wmi w = wmi.WMI()
obj = w.Win32_ComputerSystem()[0] print(obj)
instance of Win32_ComputerSystem
{
AdminPasswordStatus = 3;
AutomaticManagedPagefile = TRUE;
AutomaticResetBootOption = TRUE;
AutomaticResetCapability = TRUE;
BootOptionOnLimit = 3;
BootOptionOnWatchDog = 3;
BootROMSupported = TRUE;
BootupState = "Normal boot";
Caption = "QH-20181120YSCF";
ChassisBootupState = 3;
CreationClassName = "Win32_ComputerSystem";
CurrentTimeZone = 480;
Description = "AT/AT COMPATIBLE";
DNSHostName = "QH-20181120YSCF";
Domain = "WORKGROUP";
DomainRole = 0;
EnableDaylightSavingsTime = TRUE;
FrontPanelResetStatus = 3;
InfraredSupported = FALSE;
KeyboardPasswordStatus = 3;
Manufacturer = "Dell Inc.";
Model = "Inspiron 5439";
Name = "QH-20181120YSCF";
NetworkServerModeEnabled = TRUE;
NumberOfLogicalProcessors = 4;
NumberOfProcessors = 1;
OEMStringArray = {"Dell System", "1[0638]", "3[1.3]", "4[0001]", "6[D0, D4, D8, DA, DE]", "10[A04]", "8[]", "9[]", "10[XXX]", "13[PP36S]", "14[3]", "15[9]", "16[]"};
PartOfDomain = FALSE;
PauseAfterReset = "";
PCSystemType = 2;
PowerOnPasswordStatus = 3;
PowerState = 0;
PowerSupplyState = 3;
PrimaryOwnerName = "PC";
ResetCapability = 1;
ResetCount = -1;
ResetLimit = -1;
Roles = {"LM_Workstation", "LM_Server", "NT", "Potential_Browser"};
Status = "OK";
SystemType = "x64-based PC";
ThermalState = 3;
TotalPhysicalMemory = "";
UserName = "QH-20181120YSCF\\Administrator";
WakeUpType = 6;
Workgroup = "WORKGROUP";
};
import wmi w = wmi.WMI()
obj = w.Win32_ComputerSystem()[0] print("机器型号",obj.model)
print("制造商",obj.Manufacturer) '''
机器型号 Inspiron 5439
制造商 Dell Inc.
'''
wmi.Win32_OperatingSystem() 方法
可以使用 Win32_OperatingSystem() 获取sn号 SerialNumber
import wmi w = wmi.WMI()
obj = w.Win32_ComputerSystem()[0] print(obj)
instance of Win32_OperatingSystem
{
BootDevice = "\\Device\\HarddiskVolume1";
BuildNumber = "";
BuildType = "Multiprocessor Free";
Caption = "Microsoft Windows 7 旗舰版 ";
CodeSet = "";
CountryCode = "";
CreationClassName = "Win32_OperatingSystem";
CSCreationClassName = "Win32_ComputerSystem";
CSDVersion = "Service Pack 1";
CSName = "QH-20181120YSCF";
CurrentTimeZone = 480;
DataExecutionPrevention_32BitApplications = TRUE;
DataExecutionPrevention_Available = TRUE;
DataExecutionPrevention_Drivers = TRUE;
DataExecutionPrevention_SupportPolicy = 2;
Debug = FALSE;
Description = "";
Distributed = FALSE;
EncryptionLevel = 256;
ForegroundApplicationBoost = 2;
FreePhysicalMemory = "";
FreeSpaceInPagingFiles = "";
FreeVirtualMemory = "";
InstallDate = "20181120154345.000000+480";
LastBootUpTime = "20190221005145.164410+480";
LocalDateTime = "20190309002538.760000+480";
Locale = "";
Manufacturer = "Microsoft Corporation";
MaxNumberOfProcesses = 4294967295;
MaxProcessMemorySize = "";
MUILanguages = {"zh-CN"};
Name = "Microsoft Windows 7 旗舰版 |C:\\Windows|\\Device\\Harddisk0\\Partition1";
NumberOfLicensedUsers = 0;
NumberOfProcesses = 115;
NumberOfUsers = 1;
OperatingSystemSKU = 1;
Organization = "Microsoft";
OSArchitecture = "64-bit";
OSLanguage = 2052;
OSProductSuite = 256;
OSType = 18;
Primary = TRUE;
ProductType = 1;
RegisteredUser = "PC";
SerialNumber = "00426-OEM-8992662-00006";
ServicePackMajorVersion = 1;
ServicePackMinorVersion = 0;
SizeStoredInPagingFiles = "";
Status = "OK";
SuiteMask = 272;
SystemDevice = "\\Device\\HarddiskVolume1";
SystemDirectory = "C:\\Windows\\system32";
SystemDrive = "C:";
TotalVirtualMemorySize = "";
TotalVisibleMemorySize = "";
Version = "6.1.7601";
WindowsDirectory = "C:\\Windows";
};
import wmi w = wmi.WMI()
obj = w.Win32_OperatingSystem()[0] print("sn",obj.SerialNumber)
# sn 00426-OEM-8992662-00006
获取物理磁盘信息
wmi.Win32_DiskDrive()方法
import wmi w = wmi.WMI()
disk = w.Win32_DiskDrive()[0] print(disk)
instance of Win32_DiskDrive
{
BytesPerSector = 512;
Capabilities = {3, 4, 10};
CapabilityDescriptions = {"Random Access", "Supports Writing", "SMART Notification"};
Caption = "ST500LT0 12-1DG142 SCSI Disk Device";
ConfigManagerErrorCode = 0;
ConfigManagerUserConfig = FALSE;
CreationClassName = "Win32_DiskDrive";
Description = "磁盘驱动器";
DeviceID = "\\\\.\\PHYSICALDRIVE0";
FirmwareRevision = "";
Index = 0;
InterfaceType = "IDE";
Manufacturer = "(标准磁盘驱动器)";
MediaLoaded = TRUE;
MediaType = "Fixed hard disk media";
Model = "ST500LT0 12-1DG142 SCSI Disk Device";
Name = "\\\\.\\PHYSICALDRIVE0";
Partitions = 4;
PNPDeviceID = "SCSI\\DISK&VEN_ST500LT0&PROD_12-1DG142\\4&30B6A92E&0&010000";
SCSIBus = 1;
SCSILogicalUnit = 0;
SCSIPort = 0;
SCSITargetId = 0;
SectorsPerTrack = 63;
SerialNumber = " W3P2DXE1";
Signature = 2837888961;
Size = "";
Status = "OK";
SystemCreationClassName = "Win32_ComputerSystem";
SystemName = "QH-20181120YSCF";
TotalCylinders = "";
TotalHeads = 255;
TotalSectors = "";
TotalTracks = "";
TracksPerCylinder = 255;
};
获取硬盘sn 、硬盘大小 、型号制造商 、硬盘型号
import wmi w = wmi.WMI()
disk = w.Win32_DiskDrive()[0] print("硬盘制造商Manufacturer",disk.Manufacturer)
print("硬盘型号", disk.Model)
print("硬盘sn", disk.SerialNumber)
print("硬盘大小", int(disk.Size) / (1024 * 1024 * 1024)) '''
硬盘制造商Manufacturer (标准磁盘驱动器)
硬盘型号 ST500LT0 12-1DG142 SCSI Disk Device
硬盘sn W3P2DXE1
硬盘大小 465.7593083381653
'''
Win32_NetworkAdapterConfiguration() 方法
函数功能: 用于网络接口信息对象,并存以列表形式 返回参数: interfaceList
interfaceList: list 类型,list 中每个元素均为一个网络接口信息的 object
获取所有网卡信息
import wmi w = wmi.WMI()
data = {}
count = 0
for nic in w.Win32_NetworkAdapterConfiguration():
if nic.MACAddress is not None:
count+=1
item_data = {}
item_data['macaddress'] = nic.MACAddress
item_data['model'] = nic.Caption
item_data['name'] = nic.Index if nic.IPAddress is not None:
item_data['ipaddress'] = nic.IPAddress[0]
item_data['netmask'] = nic.IPSubnet
else:
item_data['ipaddress'] = ""
item_data['netmask'] = ""
data["nic%s" %count] = item_data print(data)
'''
{
'nic1': {
'macaddress': 'FE:F8:AE:B0:93:4D',
'model': '[00000012] Microsoft Virtual WiFi Miniport Adapter',
'name': 12, 'ipaddress': '', 'netmask': ''
}, 'nic2': {
'macaddress': 'E0:DB:55:EC:EF:DC',
'model': '[00000013] Realtek PCIe GBE Family Controller',
'name': 13,
'ipaddress': '',
'netmask': ''
}, 'nic3': {
'macaddress': 'FC:F8:AE:B0:93:51',
'model': '[00000016] Bluetooth 设备(个人区域网)',
'name': 16,
'ipaddress': '',
'netmask': ''
}, 'nic4': {
'macaddress': '00:50:56:C0:00:01',
'model': '[00000018] VMware Virtual Ethernet Adapter for VMnet1',
'name': 18,
'ipaddress': '192.168.234.1',
'netmask': ('255.255.255.0', '64')
}, 'nic5': {
'macaddress': 'FC:F8:AE:B0:93:4D',
'model': '[00000019] Intel(R) Wireless-N 7260',
'name': 19,
'ipaddress': 'x.x.x.x',
'netmask': ('255.255.255.0', '64')
}, 'nic6': {
'macaddress': '00:50:56:C0:00:08',
'model': '[00000020] VMware Virtual Ethernet Adapter for VMnet8',
'name': 20,
'ipaddress': 'x.x.x.x',
'netmask': ('255.255.0.0', '64')
}, 'nic7':
{
'macaddress': 'FE:F8:AE:B0:93:4E',
'model': '[00000023] Microsoft Virtual WiFi Miniport Adapter',
'name': 23,
'ipaddress': '',
'netmask': ''}
}
'''
python wmi模块 获取windows内部信息的更多相关文章
- python使用wmi模块获取windows下的系统信息监控系统-乾颐堂
Python用WMI模块获取Windows系统的硬件信息:硬盘分区.使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息. 本文实例讲述了python使用wmi模块获取w ...
- Python使用WMI模块获取Windows系统的硬件信息,并使用pyinstaller库编译打包成exe的可执行文件
由于公司现阶段大多数应用软件都是基于Windows系统开发和部署,很多软件安装部署都是在windows server 2012.windows server 2008之类的服务器上,部门同事每次测试一 ...
- Python的功能模块[0] -> wmi -> 获取 Windows 内部信息
wmi模块 / wmi Module WMI (Windows Management Instrumentation) 模块可用于获取 Windows 内部信息.该模块需要 win32com 的支持, ...
- Python(phone)模块获取手机号归属地、区号、运营商等
Python(phone)模块获取手机号归属地.区号.运营商等 一.我使用的是python3,可以自行搜索下载 二.安装phone模块, pip install phone 三.测试代码如下: fro ...
- python - wmi模块学习(windwos硬件信息获取)
获取windows操作系统的硬件信息 #!/usr/bin/env python # -*- coding: utf-8 -*- # http://www.cnblogs.com/liu-ke/ im ...
- Python用WMI模块获取windowns系统信息
安装vmi https://pypi.org/project/WMI/#history 脚本如下: #!/usr/bin/env python #coding:utf- import wmi impo ...
- python用WMI模块获取系统命名空间
可以和winmgmts的查询页面对应 from win32com.client import GetObject import pywintypes result=[] def enum_namesp ...
- Python requests模块在Windows下安装
发现一个爬虫库太方便了,而且支持python3! 安装方法在http://docs.python-requests.org/en/latest/user/install/#install很详细 只不过 ...
- python random模块(获取随机数)
如果要使用random模块,需要先导入 import random 1.random.random() #用于生成一个0到1的随机浮点数 2.random.uniform(a,b) #用于生成一个 ...
随机推荐
- Java中的引用类型Scanner类和随机类型Random
Scanner类 我们要学的Scanner类是属于引用数据类型,我们先了解下引用数据类型. 引用数据类型的使用 与定义基本数据类型变量不同,引用数据类型的变量定义及赋值有一个相对固定的步骤或格式. ...
- php实现多域名共享session会话
php会话机制参考:我的随笔 缘起 网站,通常会有多个服务器,多个子域名,每个节点运行着不同模块.有时为了整体体验,用户用同一个用户名.密码浏览整站,不用重复登录.这时候就需要多服务器共享sessio ...
- .Net Core 部署到Ubuntu 16.04 中的步骤
需要安装的工具 .Net Core Nginx反向代理服务器(相当于Windows下的IIS) Supervisor(进程管理工具) .Net Core安装方法请参阅 官方文档 安装完成后就可以在项目 ...
- css - 兼容适配坑点总结(。。。)
1. transform为代表的这些css3属性一定要写-webkit-,不然低版本(目前遇到的是8)的苹果,不支持. 2. x的适配 /* x */ @media only screen and ( ...
- java中Date与DateFormat的格式输出
一.DateFormat java.text.DateFormat 使用 getDateInstance 来获取该国家/地区的标准日期格式.另外还提供了一些其他静态工厂方法.使用 getTimeIns ...
- sql优化的方法
参考:https://blog.csdn.net/jie_liang/article/details/77340905 11.不要写一些没有意义的查询,如需要生成一个空表结构: select col1 ...
- TOP100summit:【分享实录-猫眼电影】业务纵横捭阖背后的技术拆分与融合
王洋:猫眼电影商品业务线技术负责人.技术专家.主导了猫眼商品供应链和交易体系从0到1的建设,并在猫眼与美团拆分.与点评电影业务融合过程中,从技术层面保障了商品业务的平稳切换,同时也是美团点评<领 ...
- 实际体验 .NET Standard 2.0 的魅力
在我们的 .net core 大迁移工程中,有些项目完成了迁移,有些还未迁移,这就带来了一个烦恼——我们自己开发的公用类库如何在 .net core 与 .net framework 项目中共享?如果 ...
- centos 7 部署 MQTT
官方教程 :头痛的是nginx 和 mqtt布局有所冲突,后台不能使用需要调整,当然是用 nginx自家的布局没问题,但是要收费 官方地址 1.由于emqttd是用Erlang语言编写的,所以,在L ...
- JAVA中的array是通过线性表还是链表实现的呢?
由于高级程序设计语言中的数组类型也有随机存取的特性,因此,通常都用数组来描述数据结构中的顺序存储结构.