Check Server Network Availablity with a VBScript Ping

There are lots of occasions where it’d be handy to know if a server (or servers) is even up before we try to execute a command on them. Often, a script will fail to execute properly or run until it times out because the server we are trying to access or run a command on, simply isn’t network accessible. It might be down, firewalled, hung, or for some other inexplicable reason you just can’t access it.

There are a few ways to check if a server is online but often I find myself just “pinging” a box to see if it responds. Well since I do this so often I decided that I’d write it up as a function so that I could just check to see if a server “IsPingable()” and save myself the hassle of using some other functionality.

This function will return “TRUE” if the server is ping-able and “FALSE” if it’s not. So we could do something like “If IsPingable(myServer) Then” do some work “Else Goto 0”. Pretty easy huh? Here is the function:

Function IsPingable(vServer)

  'This function will return TRUE or FALSE after pinging a server and
  'checking it's response.
  'This script is provided under the Creative Commons license located
  'at . It may not
  'be used for commercial purposes with out the expressed written consent

    On Error Resume Next
    Const OpenAsDefault = -2
    Const FailIfNotExist = 0
    Const ForReading = 1

    Set oShell = CreateObject("WScript.Shell")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
    sTempFile = sTemp & "\runresult.tmp"

    oShell.Run "%comspec% /c ping -n 2 " & vServer & ">" & sTempFile, 0, True

    Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, _

    sResults = fFile.ReadAll
    oFSO.DeleteFile (sTempFile)
    IsPingable = (InStr(sResults, "TTL=") > 0)
    Set oShell = Nothing
    Set oFSO = Nothing

End Function



1 thought on “Check Server Network Availablity with a VBScript Ping”

Leave a Comment