Question Locked

A Script to do the nslookup ??

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

It is one of my routine job to confrim the DNS entries resolving.

I normally do that by giving the command

nslookup DNSname/IP Address DNS-Server-IP
---------------------------- -------------------

I find this task is time consuming..when there are about 100 entries to check. Can anybody advice a better solution ?. I would be very much grateful.

7 replies

by michael

What scripting tool(s) do you have available? I might put all the entries in a file, then use a Perl script that uses that file as an input for a short FOREACH loop routine, outputting to screen or output file. Let me know if you need help with the Perl code, or have another script language in mind.

Michael

by padma_chandran07

I have got perl available. Will you be able to write a perl code for this?.

I have downloaded perl and just started to learn it by myself.

by michael

Here's a basic Perl script to print the results to the screen (no error checking):
[pre]
#!/usr/bin/perl

open (HOSTLIST,"lists.hosts");
@hosts = ;

foreach $host(@hosts) {
$results = `nslookup $host`;
chomp ($host);
print ("Results for $host:\n");
print ("=" x 50,"\n");
print ("$results\n\n");
}
close (HOSTLIST);
[/pre]
Modify the nslookup command between the backticks to meet your needs. The above assumes you've created a file called "lists.hosts" ahead of time. You could also use @ARGV to capture the input file name from the command line if you'd rather.

If you want to capture the results to a file instead of the screen, then at the top of the script open an output file:

open (RESULTFILE,">results.txt"); #open for overwrite
or
open (RESULTFILE,">>results.txt"); #open for append

Then change all your output print commands to:

print (RESULTFILE "$results & text/variables here");

Finally, include the output file in your close command:

close (HOSTFILE,RESULTFILE);

Get the O'Reilly book "Learning Perl" if you haven't already. It's a great starting point. Let me know if you need anything else.

Michael

by ashij

Hey Padma, fomr whatI understand, You have a list of IPs (100) and you'd like to run an nslookup on all of them without having to repeat the steps everytime. So I thought maybe you could do it with a batch script. I know you wanna learn Perl, but maybe you can do that later as well. Here's the Batch script (I will not take credit for this code, originally not from me):

[pre]

@echo off
if exist list.txt del list.txt

:: list of name servers
for %%i in (69.54.120.1xx
69.54.120.2xx ) do (

:: list of sites
for %%j in (192.168.xxx.1xx
192.168.xxx.2xx
192.168.xxx.3xx
192.168.xxx.4xx ) do (

for /f "tokens=1*" %%k in ('nslookup %%j %%i') do (
if [%%k]==[Server:] set server=%%l
if [%%k]==[Address:] set address=%%l
if [%%k]==[Addresses:] set address=%%l
if [%%k]==[Name:] set name=%%l)
call echo %date% , %%server%% , %%name%% , %%address%%>>list.txt
)
)

[/pre]
[u]Explanation:[/u]
list.txt is the name of file that the result will get saved to. This file usually gets saved under the user-account name (c:\documents and settings\%username%\)

the address : 69.54.120.1xx and 2xx are the two DNS servers

The addresses: 192.168.20.1xx, 2xx .. are the IP's that you want to get resolved.

To use, save everything startingfrom "@ECHO" till the two "))" into a filename.bat and just execute.

Hope this helps.

Ash

edit:making it pretty

by shah_appu

@echo off
if exist list.txt del list.txt

for %%i in (69.54.120.1xx
69.54.120.2xx ) do (

for %%j in (192.168.xxx.1xx
192.168.xxx.2xx
192.168.xxx.3xx
192.168.xxx.4xx ) do (

for /f "tokens=1*" %%k in ('nslookup %%j %%i') do (
if [%%k]==[Server:] set server=%%l
if [%%k]==[Address:] set address=%%l
if [%%k]==[Addresses:] set address=%%l
if [%%k]==[Name:] set name=%%l)
call echo %date% , %time% , %%j , %%name%% >>list.txt
)
)

by mhd.ganji

hi
it is good
but it doesnt give all ip addresses
for example google has returned 5 addresses in my nslookup run
but the script writes just one
how should be the correction ?

This discussion is closed to new replies.