Re: Batch file for Ping?
- From: "Linn Kubler" <lkubler@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 21 Sep 2006 08:20:29 -0500
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
.
- Follow-Ups:
- Re: Batch file for Ping?
- From: Richard Mueller
- Re: Batch file for Ping?
- References:
- Batch file for Ping?
- From: Linn Kubler
- Re: Batch file for Ping?
- From: Richard Mueller
- Batch file for Ping?
- Prev by Date: Re: Re: send emails when a new file is created
- Next by Date: Re: Batch file for Ping?
- Previous by thread: Re: Batch file for Ping?
- Next by thread: Re: Batch file for Ping?
- Index(es):
Relevant Pages
|
Loading