程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to query WMI using python (Linux - Windows)

編輯:Python

I've been playing a lot lately Ansible, Unfortunately , It is only in Linux Available on the . As a Windows user , I have to learn a lot about Linux and Python How to communicate with Windows Interactive knowledge . My goal is to make my Ubuntu Linux Machine use Python To query WMI. Let's break it down !

stay Linux Upload and download WMIC

The first task is to Windows A common query on the machine WMI class . To be in Linux Do this on , We need to download and compile WMIC package . So , Please check this GitHub Gist. For anyone too lazy to click on a link , Here is how to run it .

dpkg -i libwmiclient1_1.3.14-3_amd64.deb
dpkg -i wmi-client_1.3.14-3_amd64.deb
## Test a query to a remote computer
wmic -Utestuser%tstpass //<remote IP> "SELECT * FROM Win32_OperatingSystem"

If you see Win32_OperatingSystem Properties and values of , That would be great !

Python Medium WMI

The next step is to get Python Of WMI modular . I choose to use wmi-client-wrapper Python modular . To install it :

> sudo pip install wmi-client-wrapper

After installation , Create a Python Script to test it . Suppose you have installed Python 2.x, This is what I look like . If you have Python 3.x, Your first line may read

#!/usr/bin/python3
#!/usr/bin/python
import wmi_client_wrapper as wmi
wmic = wmi.WmiClientWrapper(username="localaccount",password="localpassword",host="<HostNameOrIpAddress>",)
output = wmic.query("SELECT * FROM Win32_Processor")
print(output)
## Save this as <FileName>.py and mark is as executable:
chmod +x <FileName>.py
## Then, we can execute the script to see if it brings back the Win32_Processor class.
[{'L2CacheSize': '0', 'VMMonitorModeExtensions': False, 'ConfigManagerErrorCode': '0', 'VoltageCaps': '0', 'PowerManagementSupported': False, 'LoadPercentage': '1', 'CreationClassName': 'Win32_Processor', 'Version': '', 'Role': 'CPU', 'CpuStatus': '1', 'SecondLevelAddressTranslationExtensions': False, 'Revision': '11527', 'Status': 'OK', 'PNPDeviceID': None, 'L2CacheSpeed': '0', 'AddressWidth': '64', 'ConfigManagerUserConfig': False, 'ErrorCleared': False, 'ProcessorId': '0F8BFBFF000206D7', 'ProcessorType': '3', 'DeviceID': 'CPU0', 'CurrentVoltage': '12', 'CurrentClockSpeed': '2600', 'Manufacturer': 'GenuineIntel', 'Name': 'Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz', 'InstallDate': None, 'Level': '6', 'SocketDesignation': 'None', 'NumberOfCores': '1', 'Caption': 'Intel64 Family 6 Model 45 Stepping 7', 'StatusInfo': '3', 'Architecture': '9', 'UniqueId': None, 'PowerManagementCapabilities': 'NULL', 'OtherFamilyDescription': None, 'Description': 'Intel64 Family 6 Model 45 Stepping 7', 'NumberOfLogicalProcessors': '1', 'Family': '179', 'ErrorDescription': None, 'UpgradeMethod': '6', 'SystemName': 'HOSTNAME', 'LastErrorCode': '0', 'ExtClock': '8000', 'Stepping': None, 'VirtualizationFirmwareEnabled': False, 'MaxClockSpeed': '2600', 'L3CacheSize': '0', 'L3CacheSpeed': '0', 'Availability': '3', 'SystemCreationClassName': 'Win32_ComputerSystem', 'DataWidth': '64'}]

" ! The output is JSON, Very rough at this point , But now , I just want it to go on . I hope this will help anyone trying to make Python stay Linux Query on the remote computer on WMI People who !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved