Mass “ping” from a locked down PC

Recently I needed to find the ‘rough’ location of hundreds of computers on the network but only had access to a locked down PC where I could not use the software I would normally use. To get around this I had to resort to VB script (yep, you can run this code on some locked down PC’s) and had the results formatted as CSV to load in to Excel for processing.

First thing, how do you run VB script on a PC? You can run VB script from the command line using cscript.exe (usually found under c:\windows\system32).

Next you need to create you VB script and save it to a file. For my mass ping I created the file “vbping.vbs” with Notepad with the contents below:

' Author: Warren Cross (warren.cross@4solveit.co.uk)
' Date: 18 July 2010
' Copyright 4SolveIT - free for use and distribution if credits maintained

Option Explicit

Dim strHost, objFSO, objTextFile, strNextLine

' Check that all arguments required have been passed.
If Wscript.Arguments.Count < 1 Then
    Wscript.Echo "Arguments  required. For example:" & vbCrLf _
    & "cscript vbping.vbs pingdatafile"
    Wscript.Quit(0)
End If

strHost = Wscript.Arguments(0)

' process data file containing server list
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    (strHost, ForReading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
	Ping(strNextLine)
    'Wscript.Echo "Server name: " & strNextLine
Loop

Function Ping(strHost)

    dim objPing, objRetStatus, avg
	dim c

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
            Ping = False
			if objRetStatus.StatusCode=11010 then
			   WScript.Echo strHost & ",response time out"
			elseif objRetStatus.ResolveAddressNames=false then
			   WScript.Echo strHost & ",failed name resolution"
			else
               WScript.Echo strHost & ",Status code is " & objRetStatus.StatusCode
			end if
        else
            Ping = True

	        'average ping time across 4 cycles - first + 3 others
            avg = objRetStatus.ResponseTime
			for c = 0 To 2
				set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
					("select * from Win32_PingStatus where address = '" & strHost & "'")
					avg = avg + objRetStatus.ResponseTime
			Next
            avg = avg / (c+1)

			Wscript.Echo objRetStatus.Address & "," & objRetStatus.ProtocolAddress & "," & avg

        end if
    next
End Function

I then created another file that contained a list of machine names or IP addresses that I wanted to ping. This file I called “pingdata.txt” and contained one machine per line like this:

192.168.1.1
www.google.com
www.google.cq

Now you can run the whole lot using the commend below. This command will also save the CSV formatted output to a results.csv file which you can load in to Excel:

cscript vbping.vbs pingdata.txt > results.csv

About Warren Cross

A contract IT professional that has been in the technology industry a number of years at various blue chip companies. During his career he has been a successful application developer, analyst, project manager, IT manager and system administrator, and performed in both global and local roles. For contract opportunities feel free to contact Warren through this site.
This entry was posted in Microsoft. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *