Testing IP Connectivity With WMI

Friday Jan 17th 2003 by Marcin Policht

When writing a script to perform a task on a remote computer, it is beneficial to know whether a network connection to this computer can be established. Marcin Policht's latest article explains how to use the properties of Win32_PingStatus class to test network connectivity. The article also features a sample script that retrieves various connection-related statistics.

When writing a script to perform a task on a remote computer, it is beneficial to know whether a network connection to this computer can be established.

In such cases, the most commonly performed test is running the PING program, which generates one or more ICMP packets. The packets' return to originating computer enables the user to determine not only existence of the network path to the remote computer, but also a number of its characteristics, such as speed and reliability. This determination comes in very handy if, for example, a specific task must be performed repetitively on a large number of computers.

Before the task is executed (which typically happens as the result of a communication session on a higher level in terms of the OSI model), you can test the basic (network layer) connectivity. If this test fails, there is no reason to continue the connection attempt. You can simply log the attempt as unsuccessful and repeat the same process with the next computer. This type of approach saves time and computing power.

Unfortunately, until recently, testing results of the PING command executed as part of a script required fairly cumbersome workarounds. Typically, this was done by parsing the outcome of the command for lines indicating a failure (e.g., "Request timed out") or success (e.g., "Reply from"). This wasn't very efficient since it required redirecting the results into a temporary text file and, subsequently, reading it line by line.

With the release of Windows XP, Microsoft introduced a new Windows Management Instrumentation (WMI) provider (i.e., a software component that provides a specific type of functionality). The WMI provider gives access to status information generated by the PING command. Called (not surprisingly) Ping provider, it supports single class (Win32_PingStatus) and two methods (ExecQueryAsync and GetObjectAsync). The focus of this article will be using the properties of Win32_PingStatus class to test network connectivity.

Note that the provider is available only on the Windows XP platform (as well as on the upcoming Windows .NET). This does not mean, however, that the script must be run from a computer running Windows XP. As long as a Windows XP computer is somewhere on a network and you have administrative rights to it, you can initiate PING remotely and retrieve its results.

This opens a range of other possibilities. For example, you can sit at your desk and test characteristics of IP connectivity (e.g., reliability and speed) between two remote locations -- as long as one of them has at least one computer running Windows XP.

The following script illustrates how to use the StatusCode property of the Win32_PingStatus class to determine whether a remote computer is responding to the PING command.

A value of the StatusCode equal to 0 indicates the PING command was successful; a non-zero value indicates a failure.

The reason for the failure can be determined by analyzing returned value, which can be one of the following:

  • 11001 Buffer Too Small
  • 11002 Destination Net Unreachable
  • 11003 Destination Host Unreachable
  • 11004 Destination Protocol Unreachable
  • 11005 Destination Port Unreachable
  • 11006 No Resources
  • 11007 Bad Option
  • 11008 Hardware Error
  • 11009 Packet Too Big
  • 11010 Request Timed Out
  • 11011 Bad Request
  • 11012 Bad Route
  • 11013 TimeToLive Expired Transit
  • 11014 TimeToLive Expired Reassembly
  • 11015 Parameter Problem
  • 11016 Source Quench
  • 11017 Option Too Big
  • 11018 Bad Destination
  • 11032 Negotiating IPSEC
  • 11050 General Failure

Besides testing the status, it is possible to retrieve other connection statistics, such as packet size, response time, or remaining time to live (all returned by default in the Windows implementation of the PING). These statistics are included in the sample script below.

The sTarget variable contains the name or IP address of the target computer. If you use the name, be sure that it can be properly resolved via DNS, WINS, or local hosts (or lmhosts) file. If the name is used, in addition to returning status and parameters of the connection, the script returns the corresponding IP address.

Dim sHost		'name of Windows XP computer from which the PING command will be initiated
Dim sTarget		'name or IP address of remote computer to which connectivity will be tested
Dim cPingResults	'collection of instances of Win32_PingStatus class
Dim oPingResult		'single instance of Win32_PingStatus class

sHost		= "SWYNKPC-XP001"
sTarget		= ""

Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sHost & "/root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & _
		"WHERE Address = '" + sTarget + "'")

For Each oPingResult In cPingResults
	If oPingResult.StatusCode = 0 Then
		If LCase(sTarget) = oPingResult.ProtocolAddress Then
			WScript.Echo sTarget & " is responding"
			WScript.Echo sTarget & "(" & oPingResult.ProtocolAddress & ") is responding"
		End If
		Wscript.Echo "Bytes = " & vbTab & oPingResult.BufferSize
		Wscript.Echo "Time (ms) = " & vbTab & oPingResult.ResponseTime
		Wscript.Echo "TTL (s) = " & vbTab & oPingResult.ResponseTimeToLive
		WScript.Echo sTarget & " is not responding"
		WScript.Echo "Status code is " & oPingResult.StatusCode
	End If
Mobile Site | Full Site