• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8375
  • Last Modified:

Batch File to dump list of Hostnames to IP Addresses

Hi,  have a text file with 500 hostnames which I need to get the IP address for to create some firewall rules for.

I have seen the below batch but not sure how to apply it I have hostnames.txt and would like a simple batch file to ping all the results and then put into output.txt    HOSTNAME    xxx.xxx.xxx.xxx

Could anyone help me with that before I start doing them 1 by 1.


Regards


@echo off

setlocal

if "%~1"=="" echo Usage: %0 serverList [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 ipAddr=Not found

for /f "tokens=2 delims=[]" %%a in ('ping -n 1 %~1') do set ipAddr=%%a

echo %~1 %ipAddr% >>"%outFile%"
hostnames.txt
0
ncomper
Asked:
ncomper
1 Solution
 
sirbountyCommented:
Try this:
@echo off
for /f %%a in (c:\hosts.txt) do call :process %%a
goto :eof
 
:process
set hostname=%1
for /f "tokens=4 delims=: " %%r in ('ping -n 1 %hostname%^|find /i "Statistics"') do echo %hostname% %%r >> c:\output.txt

Open in new window

1
 
ncomperAuthor Commented:
Didnt seem to work do I just paste this into a .bat file and run it ?
0
 
sirbountyCommented:
Yes, but you'll need to change c:\hosts.txt to the path/filename you're using for input.
Also change the final line's c:\output.txt if you want it somewhere else...
1
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
gurutcCommented:
Dude Sirbounty,

That is a sweet script.  I just tried it for funsies.  You are Smart!

--> Another Question added to my Knowledgebase.

- gurutc
0
 
ncomperAuthor Commented:
Amazing thank you
0
 
sirbountyCommented:
Glad I could help both of you. :^)
Thanx for the grade!
0
 
Sp SpCommented:
Thank you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now