[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1097
  • Last Modified:

ping in vbscript


I wrote a script to get the output of ping every few minutes.
This is part of my script:

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 4 -w 1000 " & serverName)
strPingResults = LCase(objExec.StdOut.ReadAll)

Since ping runs in a window, a window pops up every few minutes when my script is running and I don't want that. I would like to get the output from pinging a server without a window showing up every few minutes. How can I do that? Should I be calling some other ping program?

Thank you
  • 4
  • 2
1 Solution
Check out http://support.microsoft.com/default.aspx?scid=kb;en-us;300197 ("HOW TO: Ping an IP Address with Visual Basic by Using ICMP"), maybe you can change that to work with VBS.
I don't know of a way to hide the window using objShell.Exec

If the link above doesn't work out for you then let me know what you would like to with the
"strPingResults" and I will post a script for you
newpersonAuthor Commented:
From strPingResults, I need to get the average response time. For example, if this is the output of ping, I need to get the number 40.

Pinging www.google.akadns.net [] with 32 bytes of data:
Reply from bytes=32 time=40ms TTL=240
Reply from bytes=32 time=40ms TTL=240
Reply from bytes=32 time=40ms TTL=240
Reply from bytes=32 time=40ms TTL=240
Ping statistics for
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 40ms, Maximum =  40ms, Average =  40ms

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


Here is a sample that will get the response time of a ping
but we have to use a loop to ping 4 times to find the average:

'!!!Begin Copy
Dim strHost, strTarget, colPingResults, objPingResult, strPingRT,i

strHost = "."
strTarget = "www.google.akadns.net"

Do Until i = 4
Set colPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
        strHost & "/root/cimv2").ExecQuery("SELECT * FROM Win32_PingStatus " & _
        "WHERE Address = '" + strTarget + "'")

For Each objPingResult In colPingResults
    If objPingResult.StatusCode = 0 Then
       Ping = True
        strPingRT = strPingRT + objPingResult.responsetime
        Ping = False
    End If
i = i + 1
wscript.echo strPingRT / 4

'!!End Copy

the line (line 12)....If objPingResult.StatusCode = 0 Then...
is there incase your script needs to know if there was a response or not.
newpersonAuthor Commented:
But does Win32_PingStatus work on Windows 2000? I am getting an error when I run this script...
Well you are right it doesn't work in W2K...sorry

The best I can come up with is to use cscript.exe to call your existing vbs file.
I have tried this on my W2k machine and it worked

Place these 2 lines in a seperate vbscript and replace c:\ExistingFile.vbs with your existing file
If there is a space in the folder path you will need to use the 8.3 format...e.g Documents and Settings = docume~1

Set wshshell = CreateObject("WScript.Shell")
wshshell.run "cmd /c cscript //nologo c:\ExistingFile.vbs",0

Good Luck!

Glad it worked out for you...thanks for the points

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now