Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Capture ouptut from Nslookup

512 views
Skip to first unread message

Chris Flesher

unread,
Sep 19, 2006, 1:08:02 PM9/19/06
to
I use the following code to run nslookup against a set of machines:

execCmd = "%comspec% /c nslookup.exe " ' Leave a space before the last quote
sourceFile = "c:\comp.txt"
targetFile = "c:\ip.txt"

forReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(sourceFile,forReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Set WshShell = WScript.CreateObject("WScript.Shell")

RetVal = WshShell.Run(execCmd & strLine & " >> " & targetfile , 1, True)
Loop
set WshShell = nothing
set objFSO = nothing

Instead of writing the output to targetfile, I'd like to get the StdOut info
and parse it so I only write the last two lines to targetfile.

Basically, I'm not getting how to use StdOut to store the output from the
command in a way that I can parse.

Gilles LAURENT

unread,
Sep 19, 2006, 3:26:53 PM9/19/06
to
"Chris Flesher" <ChrisF...@discussions.microsoft.com> a écrit dans
le message de news:33DB505A-D515-4F97...@microsoft.com

Hi Chris,

[...]

| Instead of writing the output to targetfile, I'd like to get the
| StdOut info and parse it so I only write the last two lines to
| targetfile.

Below an example of script to capture the stdout stream of the external
command nslookup. The NSLookup function will return the last two lines
in an array.

-- cut here --

Option Explicit

Dim arrRet

arrRet = NSLookup ("192.168.40.130")
WScript.Echo arrRet (0) & VbCrLf & arrRet (1)

Function NSLookup (strIPAddress)

Dim oSh, oEx
Dim strLine, arrLastTwoLines (1)

Set oSh = CreateObject ("WScript.Shell")

' execute the external command
Set oEx = oSh.Exec ("%comspec% /c nslookup " & strIPAddress)

' wait for the command to complete
Do While oEx.Status = 0
wScript.Sleep (10)
Loop

' read the output stream
Do While Not oEx.Stdout.AtEndOfStream
strLine = Trim (oEx.Stdout.Readline)
If Left (strLine, 5) = "Name:" Then

' the IP address was resolved
arrLastTwoLines (0) = strLine
arrLastTwoLines (1) = oEx.Stdout.Readline
Exit Do

End If
Loop

NSLookup = arrLastTwoLines

End Function

-- cut here --

Hope this helps. Let us know

--
Gilles LAURENT
Me contacter : http://cerbermail.com/?zoTY7ZkLcD


Chris Flesher

unread,
Sep 19, 2006, 4:45:02 PM9/19/06
to
Thanks. That really helped. I have one small question. I've spliced my
original script together with what you've given me, and I'm able to write the
first results for the output. However, I get a WSH error: Permission denied
for Line 13,1. I'm wondering why I can write once to the file, but any
subsequent attempts fail? If you have any info, I'd appreciate it. Below is
my code:

Option Explicit

Dim arrRet

' open the file that contains hostnames


Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("c:\comp.txt",1)
Do Until objFile.AtEndOfStream
strknox = objFile.ReadLine

arrRet = NSLookup (strknox)

' write to a file the ouput of the function
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO2.OpenTextFile("c:\ip.txt",8)
objFile2.WriteLine(arrRet(0))
objFile2.WriteLine(arrRet(1) & VbCrLf)

Loop

Function NSLookup (strknox)

Dim oSh, oEx
Dim strLine, arrLastTwoLines (1)

Set oSh = CreateObject ("WScript.Shell")

' execute the external command
Set oEx = oSh.Exec ("%comspec% /c nslookup " & strknox)

' wait for the command to complete
Do While oEx.Status = 0
wScript.Sleep (10)
Loop

' read the output stream
Do While Not oEx.Stdout.AtEndOfStream
strLine = Trim (oEx.Stdout.Readline)
If Left (strLine, 5) = "Name:" Then

' the IP address was resolved
arrLastTwoLines (0) = strLine
arrLastTwoLines (1) = oEx.Stdout.Readline
Exit Do

End If
Loop

NSLookup = arrLastTwoLines

End Function

Gilles LAURENT

unread,
Sep 19, 2006, 5:14:35 PM9/19/06
to
"Chris Flesher" <ChrisF...@discussions.microsoft.com> a écrit dans
le message de news:145B546B-C4CE-4BAC...@microsoft.com

| Thanks. That really helped. I have one small question. I've spliced my
| original script together with what you've given me, and I'm able to
| write the first results for the output. However, I get a WSH error:
| Permission denied for Line 13,1. I'm wondering why I can write once
| to the file, but any subsequent attempts fail? If you have any info,
| I'd appreciate it. Below is my code:

[...]

| ' write to a file the ouput of the function
| Set objFSO2 = CreateObject("Scripting.FileSystemObject")
| Set objFile2 = objFSO2.OpenTextFile("c:\ip.txt",8)
| objFile2.WriteLine(arrRet(0))
| objFile2.WriteLine(arrRet(1) & VbCrLf)

objFile2.Close

It is mandatory to close the file because you open it in the loop for
each host names. Another solution is to open the file only once before
to enter in the loop and to close it when the process is completed :

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\comp.txt",1)

Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO2.OpenTextFile("c:\ip.txt",8)

Do Until objFile.AtEndOfStream
strknox = objFile.ReadLine

arrRet = NSLookup (strknox)

objFile2.WriteLine(arrRet(0))
objFile2.WriteLine(arrRet(1) & VbCrLf)

Loop

objFile.Close
objFile2.Close

Hope this helps. Let us know.

Chris Flesher

unread,
Sep 20, 2006, 10:36:03 AM9/20/06
to
Works Great. Thanks so much for your help.
0 new messages