batch script to get dns names for set of ip a

December 30, 2010 at 05:00:13
Specs: Windows XP
Can you please help me for a batch script which gives the dns names (to a output file) from the set of ip addresses(available in input file)?

See More: batch script to get dns names for set of ip a

Report •

#1
December 30, 2010 at 07:34:22
This some old code I wrote a while back. It could probably use some refinement...

@echo off
setlocal ENABLEEXTENSIONS

set OutputFile=result.txt
set ListFile=servers.txt
echo y|del %OutputFile%

for /f %%a in (%ListFile%) do (
  call :Sub %%a)
notepad "%OutputFile%"
goto :eof

:Sub
  set IP=0.0.0.0
  set Name=localhost
  set state=Pingable
  ping %1 -n 1 > NUL
  if ERRORLEVEL 1 set state=No Reply
  if "%state%"=="Pingable" call :RDNS %1
  echo %Name%,%IP%,%state% >> "%OutputFile%"
goto :eof

:RDNS
 for /f "tokens=1,2 delims= " %%A in ('nslookup %1') do (
  if "%%A"=="Name:" call :SetName %%B
  if "%%A"=="Address:" call :SetIP %%B
  if "%%A"=="Addresses:" call :SetIP %%B)
goto :eof

:SetIP
  set IP=%1
goto :eof
:SetName
  set Name=%1
goto :eof


Report •

#2
January 20, 2011 at 12:06:09
Here's my modified version of your script, I change the order it appears (IP, Hostname, pignable/not) and it now shows the real IP it tried to ping instead of 0.0.0.0 upon failure.

@echo off
setlocal ENABLEEXTENSIONS

set OutputFile=result.txt
set ListFile=servers.txt
echo y|del %OutputFile%

for /f %%a in (%ListFile%) do (
call :Sub %%a)
notepad "%OutputFile%"
goto :eof

:Sub
set Name=Unused
set state=Pingable
ping %1 -n 1 > NUL
if ERRORLEVEL 1 set state=No Reply
if "%state%"=="Pingable" call :RDNS %1
echo %1,%Name%,%state% >> "%OutputFile%"
goto :eof

:RDNS
for /f "tokens=1,2 delims= " %%A in ('nslookup %1') do (
if "%%A"=="Name:" call :SetName %%B
if "%%A"=="Address:" call :SetIP %%B
if "%%A"=="Addresses:" call :SetIP %%B)
goto :eof

:SetIP
set IP=%1
goto :eof
:SetName
set Name=%1
goto :eof


Report •
Related Solutions


Ask Question