Solved

Batch File to dump list of Hostnames to IP Addresses

Posted on 2008-10-15
7
7,115 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 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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

Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application timeout question 2 61
Script to write to a log file 18 54
Tracing Username by IP Address or hostnames 3 88
Bat File Script - HELP 2 44
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…
Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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