Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Batch File to dump list of Hostnames to IP Addresses

Posted on 2008-10-15
7
Medium Priority
?
8,068 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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
ATEN's HDBaseT Presentation at InfoComm 2017

Hear ATEN Product Manager YT Liang review HDBaseT technology, highlighting ATEN’s latest solutions as they relate to real-world applications during her presentation at the HDBaseT booth at InfoComm 2017.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

649 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