HEELPBOOK - VBScript – IsConnectible, a Ping Method ################### ######## SCENARIO ############# Whenever I am doing large sweeps of the network that require connecting to a large number of workstations (e.g. file copy, wmi query, etc.), I prefer to check to see if I can even see the system. This avoids waiting for (WMI) timeouts and also aids in troubleshooting failures. If the file copy failed, why? Well, if I can’t ping it or it can’t be resolved, I would like to know right away and move on to the next host. Of course, there are a couple downsides to this method. It does add overhead to the script because it too has a timeout. However, depending on the purpose of the script, this may be acceptable for the flexibility you gain. The other caveat is that the systems you run this against must allow ICMP on their local firewall or the script will just ignore them and move on to the next host. There are several methods for pinging hosts but I’ve found this to be the most reliable since it works against any system that allows ICMP, even Linux or Macs. This is adapted from Richard Mueller’s ping script. This method will return three possible values: “Online”, “No Ping Reply”, or “No DNS/WINS Entry”. You can also tweak the ping command options to your liking. ######## SOLUTION ############# Here is an example of how to call the function: Dim computers(2), computer, pingable computers(0) = "pc-100.domain.local" computers(1) = "pc-200.domain.local" comptuers(2) = "pc-300.domain.local" For Each computer In computers Select Case IsConnectible(computer) Case "Online" wscript.echo computer & " is online" Case "No Ping Reply" wscript.echo computer & " is offline or firewall blocks ICMP" Case "No DNS/WINS Entry" wscript.echo computer & " cannot be found in DNS/WINS" Case "Host Unreachable" wscript.echo computer & " is unreachable" End Select Next Here is the function: Private Function IsConnectible(ByVal strComputer) ' Uses ping.exe to check if computer is online and connectible. ' Adapted from http://www.rlmueller.net/Programs/Ping1.txt Dim objShell, objExecObject, strText Set objShell = CreateObject("Wscript.Shell") ' use ping /? to find additional values for ping command; see -n and -w Set objExecObject = objShell.Exec("%comspec% /c ping -n 2 -w 750 " & strComputer) Do While Not objExecObject.StdOut.AtEndOfStream strText = strText & objExecObject.StdOut.ReadLine() Loop If InStr(strText,"could not find host") > 0 Then IsConnectible = "No DNS/WINS Entry" ElseIf (InStr(strText,"Reply from ") > 0) And (InStr(strText,": bytes=") > 0) Then IsConnectible = "Online" ElseIf InStr(strText,"Destination host unreachable") > 0 Then IsConnectible = "Host Unreachable" Else IsConnectible = "No Ping Reply" End If End Function ############ ARTICLE INFO ############# Article Month: March Article Date: 01/03/2012 Permalink: http://heelpbook.altervista.org/2012/vbscript-isconnectible-a-ping-method/ Source: http://halfloaded.com/blog/isconnectible-my-vbscript-ping-method/ Language: English View more articles on: http://www.heelpbook.net/ Follow us on Facebook: http://it-it.facebook.com/pages/HeelpBook/100790870008832 Follow us on Twitter: https://twitter.com/#!/HeelpBook Follow us on RSS Feed: http://feeds.feedburner.com/Heelpbook