Re: Batch file for Ping?



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?
    ... Thanks to you and JFord for the responses. ... Dim strIPAddress, objShell, objFSO, strTemp, strTempFile ... ' Specify temporary file to save ping results. ...
    (microsoft.public.windows.server.scripting)
  • 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: 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: Exporting from Access 2002 to Excel 2002 into multiple tabs
    ... and it highlights the first line of code "DIM qdf As DAO.QueryDef". ... you don't need a separate query for what you seek to do. ... <MS ACCESS MVP> ... Dim strSQL As String, strTemp As String ...
    (microsoft.public.access.queries)