batch file to find hostnames from ip addresses of lot of machines

I need to find the hostnames from ip addresses of almost 50 machines. So I need to open a dos window and

issue ping -a ipaddress and then I get the reply form where I can get the ip address
like this
________________________________________________________________
C:\>ping -a ipaddress

Pinging hostname [ipaddress] with 32 bytes of data:

Reply from ipaddress: bytes=32 time=2ms TTL=122
Reply from ipaddress: bytes=32 time=2ms TTL=122
Reply from ipaddress: bytes=32 time=2ms TTL=122
Reply from ipaddress: bytes=32 time=2ms TTL=122

Ping statistics for ipaddress:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 2ms, Maximum = 2ms, Average = 2ms

________________________________________________________________

Now could somebody write a batch file for me which will be

batchfile inputfilename outputfilename

int the input file I will put all the ipaddresses

In the output file it should have the output as

ipaddress        hostname
10.555.55.55  hostname
anshumaEngineeringAsked:
Who is Participating?
 
SteveGTRCommented:
Sorry, I didn't change the delims from the last batch file I did for you. Also, I didn't look real closely at my output file. My mistake.

@echo off

setlocal

if "%~1"=="" echo Usage: %0 ipList [outfile]&goto :EOF
if not exist "%~1" echo %~1 does not exist&goto :EOF

set outFile=%~2

if "%~2"=="" set outFile=outfile.txt

if exist "%outFile%" del "%outFile%"

for /f "tokens=*" %%a in ('type "%~1"') do call :PROCESS "%%a"

echo Results in %outFile%

goto :EOF

:PROCESS

set hostName=Not found

for /f "tokens=2" %%a in ('ping -a -n 1 %~1 2^>NUL ^| findstr /i Pinging') do set hostName=%%a

echo %~1 %hostName% >>"%outFile%"
0
 
SteveGTRCommented:
Try this:

@echo off

setlocal

if "%~1"=="" echo Usage: %0 ipList [outfile]&goto :EOF
if not exist "%~1" echo %~1 does not exist&goto :EOF

set outFile=%~2

if "%~2"=="" set outFile=outfile.txt

if exist "%outFile%" del "%outFile%"

for /f "tokens=*" %%a in ('type "%~1"') do call :PROCESS "%%a"

echo Results in %outFile%

goto :EOF

:PROCESS

set hostName=Not found

for /f "tokens=2 delims=[]" %%a in ('ping -a -n 1 %~1 2^>NUL ^| findstr /i Pinging') do set hostName=%%a

echo %~1 %hostName% >>"%outFile%"
0
 
anshumaEngineeringAuthor Commented:
Its not working. Its printing the ipaddresses again in the output file and the format of output is

ipaddress ipaddress
0
 
bmsjeffCommented:
komodo laboratories has a freeware program that will do this:
http://www.komodolabs.com/newtfree.shtml
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.