Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script to resolve hostnames to Ip addresses

Posted on 2009-04-09
4
Medium Priority
?
1,522 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
[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
  • 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 2000 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

688 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