Re: Batch file for Ping?
- From: "Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 22 Sep 2006 20:45:16 -0500
Linn Kubler wrote:
Richard,
Thanks to you and JFord for the responses. JFord's sollution is what I
originally had in mind but your's is much more ellegant. However, it
actually works in just the opposite maner than I was asking. I would like
to be able to scan a range of IP addresses and return the address and
computer name. I'm trying to determine all the statically assigned IP
addresses in use on my network. Would it be hard to modify this for that
functionallity?
This script only returns computers that are in the Active Directory and
not all devices with IP addresses are recorded there. Things like
switches and print servers aren't in my AD anyways.
I see your point. However, the ping can take awhile to timeout when you try
many addresses. Also, I wanted to have a list not just of IP addresses that
responded, but also the corresponding host name. I found that when I ping
with the -a option (to resolve the host name), it takes considerably longer.
My solution below is to use 2 functions. The first uses Ping without the -a
option and returns True if the IP address responds to the ping. Only if this
is True do I call the second function, which uses the -a option to resolve
the host name. The second function parses the output of the ping command for
the host name. I also reduced the timeout value to 100 milliseconds to speed
things up. Still, it takes a few minutes to check 255 IP addresses. My
solution follows:
=========
Option Explicit
Dim strIPAddress, objShell, objFSO, strTemp, strTempFile
Dim strSubNet, intStart, intEnd, strResult, k
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
' Specify temporary file to save ping results.
strTemp = objShell.ExpandEnvironmentStrings("%TEMP%")
strTempFile = strTemp & "\RunResult.tmp"
' Specify the subnet to check and the starting and ending addresses.
strSubNet = "10.10.5."
intStart = 0
intEnd = 255
' Check all possible addresses.
For k = intStart To intEnd
strIPAddress = strSubNet & CStr(k)
' Check if the addresses responds to a ping.
If (PingIP(strIPAddress, 1, 100) = True) Then
' Ping again to resolve the host name.
strResult = PingMachine(strIPAddress, 1, 100)
Wscript.Echo strIPAddress & " " & strResult
End If
Next
Function PingIP(strHost, intPings, intTO)
' Returns True if IP Address can be pinged, False otherwise.
' Based on a program by Alex Angelopoulos and Torgeir Bakken.
' Variables objFSO, objShell, and strTempFile have global scope
' and must be declared in the main program.
Dim objFile, strResults
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
If (intPings = "") Then
intPings = 2
End If
If (intTO = "") Then
intTO = 750
End If
' Ping the machine and pipe results to temporary file.
objShell.Run "%comspec% /c ping -n " & intPings & " -w " & intTO _
& " " & strHost & ">" & strTempFile, 0, True
' Read the file.
Set objFile = objFSO.OpenTextFile(strTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)
strResults = objFile.ReadAll
objFile.Close
' Check for response.
Select Case InStr(strResults, "TTL=")
Case 0
' No response.
PingIP = False
Case Else
' Host responded to ping.
PingIP = True
End Select
End Function
Function PingMachine(strHost, intPings, intTO)
' Returns host name if IP Address can be pinged.
' Based on a program by Alex Angelopoulos and Torgeir Bakken.
' Variables objFSO, objShell, and strTempFile have global scope
' and must be declared in the main program.
Dim objFile, strResults, intIndex1, intIndex2
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
If (intPings = "") Then
intPings = 2
End If
If (intTO = "") Then
intTO = 750
End If
' Ping the machine and pipe results to temporary file.
objShell.Run "%comspec% /c ping -a -n " & intPings & " -w " & intTO _
& " " & strHost & ">" & strTempFile, 0, True
' Read the file.
Set objFile = objFSO.OpenTextFile(strTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)
strResults = objFile.ReadAll
objFile.Close
' Check for response.
Select Case InStr(strResults, "TTL=")
Case 0
' No response.
PingMachine = ""
Case Else
' Host responded to ping. Parse for host name.
intIndex1 = InStr(strResults, "Pinging ")
intIndex2 = InStr(intIndex1, strResults, " [")
If (intIndex1 > 0) And (intIndex2 > intIndex1) Then
PingMachine = Mid(strResults, intIndex1 + 8, _
intIndex2 - intIndex1 - 8)
Else
PingMachine = "<unknown>"
End If
End Select
End Function
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
.
- Follow-Ups:
- Re: Batch file for Ping?
- From: Linn Kubler
- Re: Batch file for Ping?
- References:
- Batch file for Ping?
- From: Linn Kubler
- Re: Batch file for Ping?
- From: Richard Mueller
- Re: Batch file for Ping?
- From: Linn Kubler
- Batch file for Ping?
- Prev by Date: Re: net user password never expires option
- Next by Date: 5 Ways to Speed Up Your Computer's Performance
- Previous by thread: Re: Batch file for Ping?
- Next by thread: Re: Batch file for Ping?
- Index(es):
Relevant Pages
|