Solved

Batch File to dump list of Hostnames to IP Addresses

Posted on 2008-10-15
7
6,290 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Creating an OSPF network that automatically (dynamically) reroutes network traffic over other connections to prevent network downtime.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now