snsuser
asked on
Script nslookup results to output file
I am trying to edit a vbscript to input a txt file list of ipaddresses to nslookup and output the computernames into a file. My code is almost there but still not working right. It inputs the ipaddresses but returns the same ipaddresses not the computer names. It also creates a new file for each ipaddress. I would like the results to append all to one file. Any help is appreciated. Here is my code:
Const ForReading = 1
Const ForAppending = 2
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objTextFile = objFSO.OpenTextFile ("listofIPAddresses.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strIPAddress = objTextFile.Readline
If strIPAddress <> "" Then
If IsConnectible(strIPAddress , "", "") Then
strComputerName = NSlookup(strIPAddress)
If strComputerName = "" Then
strComputerName = strIPAddress
End If
Set objLogFile = objFSO.CreateTextFile _
(strComputerName & ".csv", ForWriting, True)
objLogFile.Write strComputerName
'objLogFile.Write nslookupvalue
objLogFile.Close
Else
' here you might want to report somewhere else that the script was
' not able to connect to the computer
WScript.Echo "Could not connect to IP address " & strIPAddress
End If
End If
Loop
objTextFile.Close
wscript.echo "files are done"
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Dim oShell, oFSO, sTempFile, fFile
If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750
Set oShell = CreateObject("WScript.Shel l")
Set oFSO = CreateObject("Scripting.Fi leSystemOb ject")
sTempFile = oFSO.GetSpecialFolder(2).S hortPath & "\" & oFSO.GetTempName
oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True
Set fFile = oFSO.OpenTextFile(sTempFil e, ForReading, _
FailIfNotExist, OpenAsASCII)
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)
IsConnectible = CBool(InStr(sResults, "TTL="))
End Function
Function NSlookup(sHost)
' Both IP address and DNS name is allowed
' Function will return the opposite
Set oRE = New RegExp
oRE.Pattern = "^[0-9]{1,3}\.[0-9]{1,3}\. [0-9]{1,3} \.[0-9]{1, 3}$"
bInpIP = False
If oRE.Test(sHost) Then
bInpIP = True
End If
Set oShell = CreateObject("Wscript.Shel l")
Set oFS = CreateObject("Scripting.Fi leSystemOb ject")
sTemp = oShell.ExpandEnvironmentSt rings("%TE MP%")
sTempFile = sTemp & "\" & oFS.GetTempName
'Run NSLookup via Command Prompt
'Dump results into a temp text file
oShell.Run "%ComSpec% /c nslookup.exe " & sHost _
& " >" & sTempFile, 0, True
'Open the temp Text File and Read out the Data
Set oTF = oFS.OpenTextFile(sTempFile )
'Parse the text file
Do While Not oTF.AtEndOfStream
sLine = Trim(oTF.Readline)
If LCase(Left(sLine, 5)) = "name:" Then
sData = Trim(Mid(sLine, 6))
If Not bInpIP Then
'Next line will be IP address(es)
'Line can be prefixed with "Address:" or "Addresses":
aLine = Split(oTF.Readline, ":")
sData = Trim(aLine(1))
End If
Exit Do
End If
Loop
'Close it
oTF.Close
'Delete It
oFS.DeleteFile sTempFile
If Lcase(TypeName(sData)) = LCase("Empty") Then
NSlookup = ""
Else
NSlookup = sData
End If
End Function
Const ForReading = 1
Const ForAppending = 2
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile ("listofIPAddresses.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strIPAddress = objTextFile.Readline
If strIPAddress <> "" Then
If IsConnectible(strIPAddress
strComputerName = NSlookup(strIPAddress)
If strComputerName = "" Then
strComputerName = strIPAddress
End If
Set objLogFile = objFSO.CreateTextFile _
(strComputerName & ".csv", ForWriting, True)
objLogFile.Write strComputerName
'objLogFile.Write nslookupvalue
objLogFile.Close
Else
' here you might want to report somewhere else that the script was
' not able to connect to the computer
WScript.Echo "Could not connect to IP address " & strIPAddress
End If
End If
Loop
objTextFile.Close
wscript.echo "files are done"
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Dim oShell, oFSO, sTempFile, fFile
If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750
Set oShell = CreateObject("WScript.Shel
Set oFSO = CreateObject("Scripting.Fi
sTempFile = oFSO.GetSpecialFolder(2).S
oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True
Set fFile = oFSO.OpenTextFile(sTempFil
FailIfNotExist, OpenAsASCII)
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)
IsConnectible = CBool(InStr(sResults, "TTL="))
End Function
Function NSlookup(sHost)
' Both IP address and DNS name is allowed
' Function will return the opposite
Set oRE = New RegExp
oRE.Pattern = "^[0-9]{1,3}\.[0-9]{1,3}\.
bInpIP = False
If oRE.Test(sHost) Then
bInpIP = True
End If
Set oShell = CreateObject("Wscript.Shel
Set oFS = CreateObject("Scripting.Fi
sTemp = oShell.ExpandEnvironmentSt
sTempFile = sTemp & "\" & oFS.GetTempName
'Run NSLookup via Command Prompt
'Dump results into a temp text file
oShell.Run "%ComSpec% /c nslookup.exe " & sHost _
& " >" & sTempFile, 0, True
'Open the temp Text File and Read out the Data
Set oTF = oFS.OpenTextFile(sTempFile
'Parse the text file
Do While Not oTF.AtEndOfStream
sLine = Trim(oTF.Readline)
If LCase(Left(sLine, 5)) = "name:" Then
sData = Trim(Mid(sLine, 6))
If Not bInpIP Then
'Next line will be IP address(es)
'Line can be prefixed with "Address:" or "Addresses":
aLine = Split(oTF.Readline, ":")
sData = Trim(aLine(1))
End If
Exit Do
End If
Loop
'Close it
oTF.Close
'Delete It
oFS.DeleteFile sTempFile
If Lcase(TypeName(sData)) = LCase("Empty") Then
NSlookup = ""
Else
NSlookup = sData
End If
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Looks like you've got this:
strOutput="C:\NS_Report.tx t"
If objfso.FileExists(strOutpu t) Then
Set objLogFile = objfso.OpenTextFile (strOutput, ForAppending)
Else
Set objLogFile = objfso.CreateTextFile(strO utput, ForWriting, True)
End If
in both the main code routine, as well as in the last function?
I just tried this again on my work PC and I got
server1.domain.com
????????? 'second one failed, for whatever reason...
I'm not sure why - but the first one did work.
I'm no good with regex's - so I wouldn't be of much help if that's where the problem is, but the code does seem to process correctly (well, except for the 2nd system I entered...need to check that out...)
strOutput="C:\NS_Report.tx
If objfso.FileExists(strOutpu
Set objLogFile = objfso.OpenTextFile (strOutput, ForAppending)
Else
Set objLogFile = objfso.CreateTextFile(strO
End If
in both the main code routine, as well as in the last function?
I just tried this again on my work PC and I got
server1.domain.com
????????? 'second one failed, for whatever reason...
I'm not sure why - but the first one did work.
I'm no good with regex's - so I wouldn't be of much help if that's where the problem is, but the code does seem to process correctly (well, except for the 2nd system I entered...need to check that out...)
ASKER
I updated the script as commented but still having problems. The output file is blank.
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile ("listofIPAddresses.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strIPAddress = objTextFile.Readline
If strIPAddress <> "" Then
If IsConnectible(strIPAddress
strComputerName = NSlookup(strIPAddress)
If strComputerName = "" Then
strComputerName = strIPAddress
End If
'Set objLogFile = objFSO.CreateTextFile _
'(strComputerName & ".csv", ForWriting, True)
'Set objLogFile = objFSO.CreateTextFile _
'(nslookupvalue & ".csv", ForWriting, True)
'Set objFSO = CreateObject("Scripting.Fi
'Set objLogFile = objFSO.CreateTextFile(nslo
'objLogFile.Write strComputerName
'objLogFile.Write nslookupvalue
'objLogFile.Close
strOutput="C:\NS_Report.tx
If objfso.FileExists(strOutpu
Set objLogFile = objfso.OpenTextFile (strOutput, ForAppending)
Else
Set objLogFile = objfso.CreateTextFile(strO
End If
Else
' here you might want to report somewhere else that the script was
' not able to connect to the computer
WScript.Echo "Could not connect to IP address " & strIPAddress
End If
End If
Loop
objTextFile.Close
wscript.echo "files are done"
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Dim oShell, oFSO, sTempFile, fFile
If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750
Set oShell = CreateObject("WScript.Shel
Set oFSO = CreateObject("Scripting.Fi
sTempFile = oFSO.GetSpecialFolder(2).S
oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True
Set fFile = oFSO.OpenTextFile(sTempFil
FailIfNotExist, OpenAsASCII)
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)
IsConnectible = CBool(InStr(sResults, "TTL="))
End Function
Function NSlookup(sHost)
' Both IP address and DNS name is allowed
' Function will return the opposite
Set oRE = New RegExp
oRE.Pattern = "^[0-9]{1,3}\.[0-9]{1,3}\.
bInpIP = False
If oRE.Test(sHost) Then
bInpIP = True
End If
Set oShell = CreateObject("Wscript.Shel
Set oFS = CreateObject("Scripting.Fi
sTemp = oShell.ExpandEnvironmentSt
sTempFile = sTemp & "\" & oFS.GetTempName
'Run NSLookup via Command Prompt
'Dump results into a temp text file
oShell.Run "%ComSpec% /c nslookup.exe " & sHost _
& " >" & sTempFile, 0, True
'Open the temp Text File and Read out the Data
Set oTF = oFS.OpenTextFile(sTempFile
'Parse the text file
Do While Not oTF.AtEndOfStream
sLine = Trim(oTF.Readline)
If LCase(Left(sLine, 5)) = "name:" Then
sData = Trim(Mid(sLine, 6))
If Not bInpIP Then
'Next line will be IP address(es)
'Line can be prefixed with "Address:" or "Addresses":
aLine = Split(oTF.Readline, ":")
sData = Trim(aLine(1))
End If
Exit Do
End If
Loop
'Close it
oTF.Close
'Delete It
oFS.DeleteFile sTempFile
If Lcase(TypeName(sData)) = LCase("Empty") Then
NSlookup = ""
Else
NSlookup = sData
End If
strOutput="C:\NS_Report.tx
If objfso.FileExists(strOutpu
Set objLogFile = objfso.OpenTextFile (strOutput, ForAppending)
Else
Set objLogFile = objfso.CreateTextFile(strO
End If
End Function