Results 1 to 9 of 9
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBScript - Ping (XP SP2)

    Hi,

    I'm looking for a simple VBS script that will ping a server on my LAN (by IP or Name) and return the results in a Msg box. I don't want to use a bat cmd file, but am happy to use either ping from within vbs or the WMI object (both of which I think are possible). Any pointers would be gratefully received.

    TIA,

  2. #2
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBScript - Ping (XP SP2)


  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,265
    Thanks
    2
    Thanked 187 Times in 173 Posts

    Re: VBScript - Ping (XP SP2)

    Based on the article Alan linked to, you could do it in one line:
    <pre>MsgBox CreateObject("WScript.Shell").Exec("ping 192.168.0.1").StdOut.ReadAll</pre>

    Replace IP address as required (or pass it as a parameter).
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBScript - Ping (XP SP2)

    Many thanks to both of you for that - the one liner does the job required and the WMI method provides some more functionality . Anyway of hiding the command window that appears when using the Exec method of WScript.Shell?

    Cheers,

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: VBScript - Ping (XP SP2)

    Not directly, but if you do a newsgroup search for wscript.shell exec hide window you'll find some suggestions, for example here.

  6. #6
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBScript - Ping (XP SP2)

    Many thanks - have had a quick look around and it seems to be a major point of discussion!

    I have attempted to create a script that will use WshShell.Run to open a command window, run ping and return the output to a string. My theory is that I can specify 0 as the value for the window, which makes the command window hidden. Its a bit messy and its not working at the moment, can anyone see where I'm going wrong?

    Dim pingip
    Set WshShell = Wscript.CreateObject("Wscript.Shell")
    pingip = WshShell.Run ("cmd /K & ping 192.168.0.1"), 0, False.StdOut.ReadAll
    MsgBox pingip
    Set WshShell = Nothing
    Set pingip = Nothing

    Cheers,

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: VBScript - Ping (XP SP2)

    Exec returns an object, Run only returns an integer.

    You could create a text file:

    CreateObject("Wscript.Shell").Run "cmd /c ping 192.168.0.1 > C:PingResult.txt", 0, False

    and use a FileSystemObject to read the text file. Otherwise, you'd have to create an intermediate .vbs file as described in the newsgroup thread I referred to.

  8. #8
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBScript - Ping (XP SP2)

    Ah, got you. Thanks. Have decided to use WMI and have produced the following script:

    strComputer = "."
    strTarget = "192.168.0.1"

    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
    Set colPings = objWMIService.ExecQuery ("Select * From Win32_PingStatus where Address = '" & strTarget & "'")

    For Each objPing in colPings
    If objPing.StatusCode = 0 Then
    MsgBox "Ping Received From:" & vbtab & strTarget & vbcrlf & _
    "Responding Address:" & vbtab & objPing.ProtocolAddress & vbcrlf & _
    "Bytes Sent:" & vbtab & vbtab & objPing.BufferSize & vbcrlf & _
    "Time:" & vbtab & vbtab & vbtab & objPing.ResponseTime & " ms" & vbcrlf & _
    "TTL:" & vbtab & vbtab & vbtab & objPing.ResponseTimeToLive & " seconds", 64, "Title text"
    Else
    MsgBox strTarget & " did not respond to ping.", 16, "Title text"
    End If
    Next

    Set strComputer = Nothing
    Set strTarget = Nothing
    Set objWMIService = Nothing
    Set colPings = Nothing

    Many thanks to everyone for all the pointers on this.

    Cheers,

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='rory' post='442835' date='13-Jan-2005 14:59']Based on the article Alan linked to, you could do it in one line:[/quote]
    Nice one, Rory. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •