Link to home
Start Free TrialLog in
Avatar of Amit
AmitFlag for United States of America

asked on

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
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

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%"
Avatar of Amit

ASKER

Its not working. Its printing the ipaddresses again in the output file and the format of output is

ipaddress ipaddress
komodo laboratories has a freeware program that will do this:
http://www.komodolabs.com/newtfree.shtml
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial