Solved

Batch File to dump list of Hostnames to IP Addresses

Posted on 2008-10-15
7
6,625 Views
Last Modified: 2016-11-04
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
Comment
Question by:ncomper
7 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 22719657
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
 
LVL 5

Author Comment

by:ncomper
ID: 22719744
Didnt seem to work do I just paste this into a .bat file and run it ?
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 22719753
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:gurutc
ID: 22719839
Dude Sirbounty,

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

--> Another Question added to my Knowledgebase.

- gurutc
0
 
LVL 5

Author Comment

by:ncomper
ID: 22720142
Amazing thank you
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22720163
Glad I could help both of you. :^)
Thanx for the grade!
0
 

Expert Comment

by:Sp Sp
ID: 41874153
Thank you.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bash script to unzip a file 5 54
Command to modify Registry entry 5 111
Windows Batch File - Read CSV 9 46
Check folder every for files and print them 5 38
Article by: rfc1180
The Maximum Segment size (MSS) is an important consideration when troubleshooting connectivity via the Internet/Intranet. As the packets are routed via the Internet/Intranet, the packets must traverse through multiple routers in the path between two…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question