Re: Batch file for Ping?




"Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23BiBtHr3GHA.3364@xxxxxxxxxxxxxxxxxxxxxxx
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


Richard,

Thanks so much, that works great. All I had to do was modify it to scan my
IP address range, which makes sense. It only took a minute or so to run
since I only needed to scan the first 99 addresses.

I did notice that most devices did not return a host name using the -a
option. Do you know if this is due to the fact that they do not have DNS
entries or because of the type of device? The result is I now have a list
of IP addresses in use but don't know exactly which devices each are. Any
suggestions there? It's only a dozen or two so I guess I can manually look
each up.

Thanks again,
Linn


.



Relevant Pages

  • Re: Batch file for Ping?
    ... the ping can take awhile to timeout when you try ... with the -a option (to resolve the host name), ... Const OpenAsDefault = -2 ... ' Check for response. ...
    (microsoft.public.windows.server.scripting)
  • Connectivity problem
    ... I've installed SUSE10 and everything seems to have ... I couldn't ping or connect to anything. ... an IP address but it gets no response to an ARP request for the ... responding with a "no such host" message. ...
    (alt.os.linux.suse)
  • Network connectivity problem
    ... I've installed SUSE and everything seems to have gone OK except that the ... I couldn't ping or connect to anything. ... an IP address but it gets no response to an ARP request for the ... responding with a "no such host" message. ...
    (alt.os.linux.suse)
  • Re: is host exist
    ... > How can i test if host exist, when host doesn't response for ping? ... If you mean the ping is timing out, then the presence of the timeout ...
    (comp.os.linux.networking)
  • Re: A question for the list...
    ... > virus infections that have affected networks and hosts. ... > attempts to remove the virus from the host. ... > response, ... > enterprise WLANs. ...
    (Incidents)