ping in vbscript

Posted on 2004-11-20
Last Modified: 2008-01-09

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
Question by:newperson
    LVL 86

    Expert Comment

    Check out;en-us;300197 ("HOW TO: Ping an IP Address with Visual Basic by Using ICMP"), maybe you can change that to work with VBS.
    LVL 16

    Expert Comment

    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

    Author Comment

    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 [] 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

    LVL 16

    Expert Comment


    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 = ""

    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.

    Author Comment

    But does Win32_PingStatus work on Windows 2000? I am getting an error when I run this script...
    LVL 16

    Accepted Solution

    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") "cmd /c cscript //nologo c:\ExistingFile.vbs",0

    Good Luck!

    LVL 16

    Expert Comment

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

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now