0

目标

编写一个 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 库或任何其他可能的方式远程获取远程主机的硬件信息。

4

1 回答 1

1

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 ) ) )
于 2020-03-19T09:54:23.070 回答