Re: Batch file for Ping?



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.

Thanks,
Linn

"Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:eEfHUtA3GHA.476@xxxxxxxxxxxxxxxxxxxxxxx
Hi,

If you want to know which machines have which IP address, the VBScript
program below produces a comma delimited file with NetBIOS name of the
computer and IP Address. It uses ADO to retrieve the names of all
computers joined to the domain. Run at a command prompt and redirect the
output to a text file. For example, if the program is in a file called
PingComputers.vbs, this command redirects the output to a file called
report.txt:

cscript //nologo PingComputers.vbs > report.txt

The VBScript program (watch for line wrapping):
======
Option Explicit

Dim strIPAddress, objShell, objFSO, strTemp, strTempFile
Dim strComputer, adoConnection, adoCommand, adoRecordset
Dim objRootDSE, strFiler, strAttributes, strQuery
Dim strDNSDomain

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"

' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE";)
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory for all computers.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open = "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Comma delimited list of attributes values to retrieve.
strAttributes = "sAMAccountName,distinguishedName"

' Construct the LDAP syntax query.
strQuery = "<LDAP://"; & strDNSDomain _
& ">;(ObjectCategory=computer);" & strAttributes & ";subtree"

' Execute the query.
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute

' Enumerate the recordset.
Do Until adoRecordset.EOF
' Retrieve the NetBIOS name of the computer.
strComputer = adoRecordset.Fields("sAMAccountName")
' Remove trailing "$".
strComputer = Left(strComputer, Len(strComputer) - 1)

' Ping the computer for the IP Address.
strIPAddress = PingMachine(strComputer, 1, 750)

' Output computer NetBIOS name and IP Address.
Wscript.Echo strComputer & "," & strIPAddress
adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

Function PingMachine(strHost, intPings, intTO)
' Returns IP Address if strHost 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 -n " & intPings & " -w " _
& intTO & " " & strHost & ">" & strTempFile, 0, True

' Read the file.
Set objFile = objFSO.OpenTextFile(strTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)
strResults = objFile.ReadAll
objFile.Close

' Search for IP Address in square brackets.
intIndex1 = InStr(strResults, "[")
intIndex2 = InStr(intIndex1 + 1, strResults, "]")

Select Case InStr(strResults, "TTL=")
Case 0
' No response.
PingMachine = "<not found>"
Case Else
' Computer responded to ping, parse IP Address.
If (intIndex1 > 0) And (intIndex2 > intIndex1) Then
PingMachine = Mid(strResults, intIndex1 + 1, _
intIndex2 - intIndex1 - 1)
Else
PingMachine = "<unknown>"
End If
End Select
End Function

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net



.



Relevant Pages

  • Re: Batch file for Ping?
    ... Angry IPScanner. ... Dim strIPAddress, objShell, objFSO, strTemp, strTempFile ... ' Specify temporary file to save ping results. ...
    (microsoft.public.windows.server.scripting)
  • Re: Batch file for Ping?
    ... Dim strIPAddress, objShell, objFSO, strTemp, strTempFile ... ' Specify temporary file to save ping results. ... Const OpenAsDefault = -2 ...
    (microsoft.public.windows.server.scripting)
  • Re: Batch file for Ping?
    ... Dim strIPAddress, objShell, objFSO, strTemp, strTempFile ... ' Specify temporary file to save ping results. ... ' Retrieve the NetBIOS name of the computer. ...
    (microsoft.public.windows.server.scripting)
  • Re: Export by group to a specific folder and name
    ... Dim qdf As DAO.QueryDef ... Dim strSQL As String, strTemp As String, strMgr As String ... "Ken Snell MVP" wrote: ...
    (microsoft.public.access.externaldata)
  • Re: ICMP (Ping)
    ... given your responses only repeat more of your own ... people will probe without pinging, even if many may ping first. ... > people have seen that many of the script kiddie tools do exactly what I ... them know the web server type, version, the OS type and version, and ...
    (Security-Basics)

Loading