상세 컨텐츠

본문 제목

WMI 인터페이스의 이용 (Using the WMI Interface)

Code snippets/Python

by 아이쓰 2008. 6. 21. 16:37

본문

윈도우 시스템에서 WMI 인터페이스에 WQL(SQL for WMI)문을 통해 질의하여, 시스템의 각종 정보들을 아주 쉽게 얻어올 수 있다.
In the Windows system, the WMI interface is queried via the WQL (SQL for WMI) statement, so it is very easy to get various information of the system.

WQL은 이름에서 유추할 수 있듯 일반적인 SQL문과 유사하다.
WQL is similar to a regular SQL statement, as you can guess from the name.

간단한 스크립트를 예로 들어보겠다.
Let's take a simple script as an example.


아래는 python코드이다.
Below is the python code.

import win32com.client

strComputer = "."

objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")

"""
Example Queries:

SELECT * FROM Win32_OperatingSystem
SELECT * FROM Win32_NetworkAdapter WHERE ConfigManagerErrorCode = 0
SELECT Name, Model, InterfaceType, MediaType, Size from Win32_DiskDrive
SELECT Name, Description, DriveType, FileSystem, FreeSpace, Size, VolumeSerialNumber from Win32_LogicalDisk
SELECT Description, IPAddress, IPSubnet, IPConnectionMetric, MACAddress, DefaultIPGateway FROM Win32_NetworkAdapterConfiguration WHERE DefaultTTL > 1
SELECT Name, MACAddress, ConfigManagerErrorCode, NetConnectionID FROM Win32_NetworkAdapter WHERE AdapterType = 'Ethernet 802.3'
"""

objResult = objSWbemServices.ExecQuery("""
SELECT Description, IPAddress, IPSubnet, IPConnectionMetric, MACAddress, DefaultIPGateway
FROM Win32_NetworkAdapterConfiguration
WHERE DefaultTTL > 1
""")

for itemResult in objResult:
print itemResult


다음은 javascript코드이다.
Here is the javascript code:

function GetInformation() {
      var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
      var service = locator.ConnectServer(".");
      var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapter");
      var e = new Enumerator (properties);
      document.write("<style type='text/css'> * { font-size: 12px; } </style><table border=0 cellspacing=1 cellpadding=5 bgcolor=0 width=100%>");
      dispHeading();
      for (;!e.atEnd();e.moveNext ())
      {
            var p = e.item ();
if (p.NetConnectionStatus != null) {
document.write("<tr bgcolor=#ffffff>");
document.write("<td>" + p.Name + " / " + p.NetConnectionID + "</td>");
document.write("<td>" + p.NetConnectionStatus + "</td>");
document.write("<td>" + p.PermanentAddress   + "</td>");
document.write("<td>" + p.MACAddress + "</td>");
document.write("</tr>");
}
      }
      document.write("</table>");
}

function dispHeading()
{
      document.write("<thead bgcolor=#efefef>");
      document.write("<td>Name</td>");
      document.write("<td>x</td>");
      document.write("<td>Status</td>");
      document.write("<td>MACAddress</td>");
      document.write("</thead>");
}

GetInformation();


델파이에서는 이렇게..
In Delphi ...
http://www.magsys.co.uk/delphi/magwmi.asp

가져오고 싶은 정보를 어디서 가져와야 할 지 모르겠다면,
If you do not know where to fetch the information you want to import,

WMI Explorer라는 프로그램을 첨부하니 다운받아서 실행해보기 바란다.
Attach a program called WMI Explorer and download and run it.

wmi_explorer.zip


'Code snippets > Python' 카테고리의 다른 글

놀고있는 GPS수신기의 활용  (0) 2013.07.31
타임서버에서 시간 가져오기 in python  (0) 2008.05.01

관련글 더보기