So I've seen a few requests lately concerning the ability to ping to check for connectivity within a vbscript. Most implementations I've seen call ping.exe using the exec method of wscript.shell and read the output via stdout. While this works just fine, I think using WMI to ping is cleaner and easier to work with. There is no need to read the output and try to extract what you want from a string like you do when using ping.exe. I hope some find this useful and if you have other Ping related ideas, let me know.
The
Reachable function will return
True or False depending on whether the address provided is reachable or not. The address can be provided with an IP address or Computer Name
The ResolveIP function will return an
IPAddress that corresponds to the Computer Name you provide
Added 1/29/07 - I put together a simple
"Connectivity Monitor" using Win32_PingStatus (I've attached a screen shot of what it looks like). You can download it from here (I put it in a zip because of the images used for it):
ConnectivityMonitor Reference for this class:
http://msdn2.microsoft.com/en-us/library/aa394350.aspx NOTE: WMI is
NOT required on the host being ping'd.
Reachable Function (WinXP/Win2k3)
'==========================================================================
' The following function will test if a machine is reachable via a ping
' using WMI and the Win32_PingStatus Class
'==========================================================================
If Reachable("10.50.138.48") Then
WScript.Echo "Computer is Reachable!"
Else
WScript.Echo "Computer is Unreachable!"
End If
Function Reachable(strComputer)
' On Error Resume Next
Dim wmiQuery, objWMIService, objPing, objStatus
wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery)
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
Reachable = False 'if computer is unreacable, return false
Else
Reachable = True 'if computer is reachable, return true
End If
Next
End Function
Reachable Function - Non WMI Version (Win2k/WinXP/Win2k3)
If Reachable("10.50.138.48") Then
WScript.Echo "Computer is Reachable!"
Else
WScript.Echo "Computer is Unreachable!"
End If
Function Reachable(strComputer)
' On Error Resume Next
Dim objShell, objExec, strCmd, strTemp
strCmd = "ping -n 1 " & strComputer
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strCmd)
strTemp = UCase(objExec.StdOut.ReadAll)
If InStr(strTemp, "REPLY FROM") Then
Reachable = True
Else
Reachable = False
End If
End Function
ResolveIP Function (WinXP/Win2k3)
'==========================================================================
' The following function will resolve a computer name to its ip address
' using WMI and the Win32_PingStatus Class
'==========================================================================
WScript.Echo ResolveIP("HOSTNAME")
Function ResolveIP(strComputer)
Dim wmiQuery : wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Dim objPing : Set objPing = objWMIService.ExecQuery(wmiQuery)
Dim objStatus
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
ResolveIP = "Computer is Unreachable!"
Else
ResolveIP = objStatus.ProtocolAddress
End If
Next
End Function
ResolveIP Function (Win2k/WinXP/Win2k3)
WScript.Echo ResolveIP("Computer1")
Function ResolveIP(computerName)
Dim objShell : Set objShell = CreateObject("WScript.Shell")
Dim objExec : Set objExec = objShell.Exec("ping " & computerName & " -n 1")
Dim strOutput : strOutput = objExec.StdOut.ReadAll
Dim RegEx : Set RegEx = New RegExp
RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
RegEx.Global = True
If RegEx.Test(strOutput) Then
ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
Else
ResolveIP = "IP Address could not be resolved."
End If
End Function
[image]local://11180/2BF856EFE52B457E953D3AD42CE190F2.jpg[/image]
<message edited by dm_4ever on Monday, May 19, 2008 8:48 AM>