Operating System - HP-UX
1753525 Members
5742 Online
108795 Solutions
New Discussion юеВ

nslookup on list of IPs stored in a file and storing resolved hostname in a file

 
SOLVED
Go to solution
Shivkumar
Super Advisor

nslookup on list of IPs stored in a file and storing resolved hostname in a file

Hi,

I have a file which as some hundred's IPs stored in a text file.
The IPs are stored in following format:

192.12.10.[10|12|14|15|16|19]|10.11.21.[9|14|17|16|90]

The IPs are delimited by | pipe character.
I need a shell script which can perform nslookup on each of IP and put the resolved instance/host name in a file.

Appreciate if anyone can suggest a shell/perl script to accomplish above.

Thanks,
Shiv
10 REPLIES 10
Dennis Handly
Acclaimed Contributor

Re: nslookup on list of IPs stored in a file and storing resolved hostname in a file

You might want to explain the format a little more.
You seem to have "|" both inside and outside []. It seems you have two patterns:
192.12.10.[10|12|14|15|16|19]
10.11.21.[9|14|17|16|90]
Shivkumar
Super Advisor

Re: nslookup on list of IPs stored in a file and storing resolved hostname in a file

Yes. There are 2 patterns. Each subnet is separated by | pipe.
Also each subnet has IPs separated by | inside []
VK2COT
Honored Contributor

Re: nslookup on list of IPs stored in a file and storing resolved hostname in a file

Hello,

Hmm, it was along day for me (teaching
HP-UX SNA II) but here is a quick
run.

a) I created a file myhosts-itrc-test.txt
that contains the following patters:

192.12.10.[10|12|14|15|16|19]|10.11.21.[9|14|17|16|90]
172.16.55.10
15.98.47.[240]
203.18.150.[1|2|21]
201.46.77.[34|95|48]|3.44.5.7

b) Then, a simple Perl script (I did not
spend much time to make it beautiful):

#!/usr/bin/perl

$MYFILE = "./myhosts-itrc-test.txt";

if ( open( MYT, "cat $MYFILE | " ) ) {
while () {
$SUBN = q{};
@MYARR = ();
@MYARR2 = ();

$_ =~ s/^\s+//g;
$_ =~ s/\s+$//g;
@MYARR = split(/\|/, $_);
foreach $ent (@MYARR) {
$FHOST = q{};
if ( grep(/\[/, $ent) ) {
@MYARR2 = split(/\[/, $ent);
$SUBN = $MYARR2[0];
$FHOST = $MYARR2[1];
$FHOST =~ s/\]//g;
$FHOST =~ s/\[//g;
}

$ent =~ s/\]//g;
$ent =~ s/\[//g;

if ( grep(/\./, $ent) ) {
print "$ent\n";
}
elsif ( "$FHOST" ) {
print "$SUBN${FHOST}\n";
}
else {
print "$SUBN${ent}\n";
}
}
}
close(MYT);
}

c) Running the script reveals the following results:

192.12.10.10
192.12.10.12
192.12.10.14
192.12.10.15
192.12.10.16
192.12.10.19
10.11.21.9
10.11.21.14
10.11.21.17
10.11.21.16
10.11.21.90
172.16.55.10
15.98.47.240
203.18.150.1
203.18.150.2
203.18.150.21
201.46.77.34
201.46.77.95
201.46.77.48
3.44.5.7

The nslookup or whatever test on each
IP address is left to you as an exercise.

Cheers,

VK2COT
VK2COT - Dusan Baljevic
James R. Ferguson
Acclaimed Contributor

Re: nslookup on list of IPs stored in a file and storing resolved hostname in a file

Hi Shiv:

Here's one solution:

# cat ./myprobe
#!/usr/bin/perl
use strict;
use warnings;
my ( $base, $subnet );

sub probe {
my ($who) = @_;
my $lookup = `nslookup $who`;
print "$who -> $1\n" if $lookup=~m{Name:\s+(.+)}i;
}
while (<>) {
chomp;
s/\|/ /g;
my @F = split /]/;
for my $range (@F) {
$range =~ s/^\s+//;
$range =~ s/\s+$//;
if ( $range =~ m{ (\d+\.\d+\.\d+\.\d+) }x ) {
probe($range);
}
( $base, $subnet ) = $range =~ m{ (\d+\.\d+\.\d+\.) \[ (.+) }x;
next unless defined $subnet;
my @FF = split / /, $subnet;
for my $node (@FF) {
probe( $base . $node ) if defined $node;
}
}
}
1;

...

Run as:

# ./myprobe inputfile

Using an 'inputfile' like:

192.12.10.[10|12|14|15|16|19]|10.11.21.[9|14|17|16|90]
10.20.30.1
10.20.30.2
10.20.30.[3]|10.21.30.3

...might produce:

*** mydns.xyz.net can't find 192.12.10.10:Non-existent host/domain
*** mydns.xyz.net can't find 192.12.10.12:Non-existent host/domain
192.12.10.14 -> ser2-v62.gw.utexas.edu
*** mydns.xyz.net can't find 192.12.10.15:Non-existent host/domain
*** mydns.xyz.net can't find 192.12.10.16:Non-existent host/domain
*** mydns.xyz.net can't find 192.12.10.19:Non-existent host/domain
*** mydns.xyz.net can't find 10.11.21.9:Non-existent host/domain
*** mydns.xyz.net can't find 10.11.21.14:Non-existent host/domain
*** mydns.xyz.net can't find 10.11.21.17:Non-existent host/domain
*** mydns.xyz.net can't find 10.11.21.16:Non-existent host/domain
*** mydns.xyz.net can't find 10.11.21.90:Non-existent host/domain
10.20.30.1 -> host1.xyz.net
10.20.30.2 -> host2.xyz.etm
10.20.30.3 -> host3.xyz.etm
10.21.30.3 -> host4.xyz.net

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: nslookup on list of IPs stored in a file and storing resolved hostname in a file

Hi (again) Shiv:

This version is cleaner and fixes a "forest-for-the-trees" error :-)

#!/usr/bin/perl
use strict;
use warnings;
my ( $base, $subnet );
sub probe {
my ($who) = @_;
my $lookup = `nslookup $who`;
print "$who -> $1\n" if $lookup=~m{Name:\s+(.+)}i;
}
while (<>) {
chomp;
s/\|/ /g;
my @F = split /]/;
for my $range (@F) {
$range =~ s/^\s+//;
$range =~ s/\s+$//;
if ( $range =~ m{ (\d+\.\d+\.\d+\.\d+) }x ) {
probe($range);
next;
}
( $base, $subnet ) = $range =~ m{ (\d+\.\d+\.\d+\.) \[ (.+) }x;
my @FF = split / /, $subnet;
for my $node (@FF) {
probe( $base . $node );
}
}
}
1;

Regards!

...JRF...
Shivkumar
Super Advisor

Re: nslookup on list of IPs stored in a file and storing resolved hostname in a file

James,

I just executed your revised version of the the script but it doesn't produce any output either on screen or in any file.

Thanks,
Shiv
James R. Ferguson
Acclaimed Contributor

Re: nslookup on list of IPs stored in a file and storing resolved hostname in a file

Hi (again) Shiv:

# I just executed your revised version of the the script but it doesn't produce any output either on screen or in any file.

Did you run the script specifying an input file like?

# ./myprobe file

Does your input file look like your original post suggested?

If neither of these questions are satisfied, _POST_ a _TEXT_ attachment of what your input file conists.

Regards!

...JRF...

Shivkumar
Super Advisor

Re: nslookup on list of IPs stored in a file and storing resolved hostname in a file

I am using command:
$./myprog testfile

$cat testfile
192.12.10.[10|12|14|15|16|19]|10.11.21.[9|14|17|16|90]
10.20.30.1
10.20.30.2
10.20.30.[3]|10.21.30.3
=====
$cat myprog
#!/usr/bin/perl
use strict;
use warnings;
my ( $base, $subnet );
sub probe {
my ($who) = @_;
my $lookup = `nslookup $who`;
print "$who -> $1\n" if $lookup=~m{Name:\s+(.+)}i;
}
while (<>) {
chomp;
s/\|/ /g;
my @F = split /]/;
for my $range (@F) {
$range =~ s/^\s+//;
$range =~ s/\s+$//;
if ( $range =~ m{ (\d+\.\d+\.\d+\.\d+) }x ) {
probe($range);
next;
}
( $base, $subnet ) = $range =~ m{ (\d+\.\d+\.\d+\.) \[ (.+) }x;
my @FF = split / /, $subnet;
for my $node (@FF) {
probe( $base . $node );
}
}
}
1;
====
It doesn't show any output.
James R. Ferguson
Acclaimed Contributor
Solution

Re: nslookup on list of IPs stored in a file and storing resolved hostname in a file

Hi (again) Shiv:

With regard to your last post, I certainly get output:

*** xxx.yyy.com can't find 192.12.10.10:Non-existent host/domain
*** xxx.yyy.com can't find 192.12.10.12:Non-existent host/domain
192.12.10.14 -> ser2-v62.gw.utexas.edu
...

...with 'xxx.yyy'com' being my DNS server disguised.

D you have 'nslookup' in your path? If this this is indeed HP-UX it would be '/usr/bin/nslookup'. If it isn't HP-UX, modify my script to reflect the absolute path:

For example, you might need to change:

my $lookup = `nslookup $who`;

to :

my $lookup = `/usr/bin/nslookup $who`;

or :

my $lookup = `/bin/nslookup $who`;

Regards!

...JRF...