Scripting NSLookup with Output to CSV

I recently needed to get IP addresses for a number of host names.  In trying to do this, I found that it was very difficult to find a script that would do what I needed.  So, after much searching and work, here is the script that I have come up with.  Hope this helps someone.


‘ NAME: NSLookupwithCSV.vbs

‘VERSION:  1.0.0
‘MODIFIED:  Doug Neely
‘DATE:  02/13/09
‘       This script will use a csv file and run it through nslookup.
‘        It will output the text to a CSV file with the hostname,ipaddress

‘This was originally found on an internet forum and modified by me.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
‘Configuration Block
‘ Input file location
strCSVFileLocation = "C:\Scripts\" ‘KEEP TRAILING SLASH!
‘ Full path to input file

strInputCSV = strCSVFileLocation & "DNSInput.csv"
‘ Full path to output file
strOutPut = strCSVFileLocation & "DNSLookup.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (strInputCSV, ForReading)

Do Until objTextFile.AtEndOfStream

   strIPAddress = objTextFile.Readline
   If strIPAddress <> "" Then

       strComputerName = NSlookup(strIPAddress)
       ‘If strComputerName = "" Then
         strComputerName = strIPAddress
       ‘End If

       ‘ here you might want to report somewhere else that the script was
       ‘ not able to connect to the computer

       WScript.Echo "Empty Field " & strIPAddress
    End If


wscript.echo "files are done"

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.Shell")
   Set oFS = CreateObject("Scripting.FileSystemObject")

   sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
   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

   ‘Close it
   ‘Delete It
   oFS.DeleteFile sTempFile

   If Lcase(TypeName(sData)) = LCase("Empty") Then
     NSlookup = ""
     NSlookup = sData
   End If

‘This will create an output file if it doesn’t exist and open it for writing
If objfso.FileExists(strOutput) Then
  Set objLogFile = objfso.OpenTextFile (strOutput, ForAppending)
  Set objLogFile = objfso.CreateTextFile(strOutput, ForWriting, True)
End If
‘This is the information that it will put into the text file
‘It includes the hostname and the IP Address (or vice versa)

  objLogFile.WriteLine sHost & "," & sData
End Function