Solved

Script to resolve hostnames to Ip addresses

Posted on 2009-04-09
4
1,417 Views
Last Modified: 2013-11-09
Hello,
do you have or know how to do a simple script that:

-reads a .txt file with hostnames, one per line
-creates a .txt or CSV file with the IP addresses one per line or hostname and IP addresses in one line, one per line.

Thanks
0
Comment
Question by:security_guy
  • 2
  • 2
4 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 24107063
Paste the script below into a text file with a .cmd extension.  Customize the value of the input variable on line 4 with the location of the list of hostnames.  Running the script will resolve the names and write output to a .csv file.


@echo off
setlocal enabledelayedexpansion
 
set input=computerlist.txt
set output=report.csv
 
for /F %%G in ('type "%input%"') do (
 set ip=
 for /F "tokens=2 skip=1" %%H in ('nslookup %%G 2^>NUL^|find "Address:"') do (
  set ip=%%H
 )
 echo %%G,!ip!>>"%output%"
)

Open in new window

0
 

Author Comment

by:security_guy
ID: 24107419
oh,, thank you a lot!!

Anyway i can see that it resolves only some hosts. For example my hostname is not resolving, google, and other web sites are not resolving.
Are not resolving web sites with only 1 address and also those with more than one address.

But it resolved "localhost" :)
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 24108657
Revised to handle multiple addresses.


@echo off
setlocal enabledelayedexpansion
 
set input=computerlist.txt
set output=report.csv
 
for /F %%G in ('type "%input%"') do (
 set ip=
 
 for /F "tokens=* skip=2" %%H in ('nslookup %%G 2^>NUL') do (
  set line=%%H
  if "!line:~0,8!"=="Address:" set ip=!line:~10!
  if "!line:~0,10!"=="Addresses:" set ip=!line:~12!
 )
 
 echo %%G,!ip!>>"%output%"
)

Open in new window

0
 

Author Comment

by:security_guy
ID: 24109575
Thank you very much!!!!!!!!!!!!!!!!!!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

831 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