python - 如何使用 WMI -(Windows Management Instrumentation)从 python 脚本获取远程系统硬件信息
问题描述
目标
编写一个 python 脚本来获取远程主机的一些硬件信息(仅限 Windows),我正在使用 wmi 库连接到远程主机硬件信息:
GPU Serial Number
Operating system version
GPU model Name
processor name
我的环境
language - python 3
connecting remote hosts using wmi library (works)
remote hosts operating system: windows 7 or windows 10
问题
当我运行下面的代码时,它会产生 100 个类/函数,我什至不知道用它来满足我的需求(获取硬件信息)
代码
import wmi
conn = wmi.WMI()
for classes in conn.classes :
print(classes)
...... ...... ...... Win32_VideoConfiguration Win32_LoggedOnUser CIM_CompatibleProduct Win32_PnPDevicePropertyReal64Array Win32_AccountSID MSFT_NetCircularDependencyDemand CIM_BootOSFromFS Msft_WmiProvider_GetObjectAsyncEvent_Post Win32_SystemSystemDriver CIM_InstIndication ...... ...... ......
最后 如何使用 wmi 库或任何其他可能的方式远程获取远程主机的硬件信息。
解决方案
The wmi
documentation is targeted for developers and IT administrators. You need to know where to find appropriate classes and their desired properties. The following commented script could help.
import wmi
conn = wmi.WMI() # or # wmi.WMI("some_other_machine")
# Operating system & OS version
for os in conn.Win32_OperatingSystem():
print( 'OS : ' + os.Caption + ", version " + os.Version )
# Processor name
for pr in conn.Win32_Processor():
print( 'CPU: ' + pr.Name )
# GPU model Name
# GPU Serial Number - partial solution
for vc in conn.Win32_VideoController():
print( 'GPU: ' + vc.Name + "\r\n " + vc.PNPDeviceID )
Please note that GPU Serial Number could be extracted from PNPDeviceID
only if the hardware manufacturer implements it:
Looking at the PNPDeviceID value, break it up by "\".
- The first piece it the bus type. For me, it is PCI.
- The second section describes the card. There's a vendor code, model number, etc.
- The last section contains a number separated by ampersands. The serial number is the second number in that list, formatted in hex.
Additional request: monitor details like serial Number, service tag, model name.
import wmi
conn = wmi.WMI()
# convert uint16[] array to string
def cnvrt( tup ):
return ''.join( [chr( x ) if x else '' for x in tup] )
# this is 'universal' DesktopMonitor (no useful details for Generic PnP Monitor?)
for umn in conn.Win32_DesktopMonitor():
print( 'UMn: Name {}'.format( umn.Name ) )
print( 'UMn: PNPDeviceID {}'.format( umn.PNPDeviceID ) )
# this is 'specific' DesktopMonitor (all useful details?)
con2 = wmi.WMI(namespace='root/WMI')
for mon in con2.WmiMonitorID():
print( 'Mon: Active {}'.format( mon.Active ) )
print( 'Mon: InstanceName {}'.format( mon.InstanceName ) )
print( 'Mon: ManufacturerName {}'.format( cnvrt( mon.ManufacturerName ) ) )
print( 'Mon: ProductCodeID {}'.format( cnvrt( mon.ProductCodeID ) ) )
print( 'Mon: SerialNumberID {}'.format( cnvrt( mon.SerialNumberID ) ) )
print( 'Mon: UserFriendlyName {}'.format( cnvrt( mon.UserFriendlyName ) ) )
推荐阅读
- android - 某些设备上的自定义工具栏内容被截断
- html - 如何在 Bootstrap 4 网格系统中将页脚内容居中?
- ruby-on-rails - 我怎样才能急切地加载和加入多态的条件?
- c++ - 如果我有五个 Win32 静态控件,如何将其中一个设置为特定的前景色?
- javascript - javascript单例公共方法“不是函数”
- jsgrid - loadData 不起作用
- python - __new__ 在对象创建中没有被调用
- xcode8 - XCUI 测试因异步等待失败而失败:超过 30 秒的超时,未达到预期
- java - Azure - 尝试从 keyVault 获取密钥时 ApplicationTokenCredential 中出现 NoSuchMethodExist 错误
- variables - 在tensorflow中,如何初始化部分变量