Perl help

mosdef

Banned
May 14, 2000
2,253
0
0
I want to write a Perl script that reads an IP address from a file and returns the computer's name. I know I need to use nslookup, but how exactly would I use it to return just the name? Thanks.

-mosdef
 

UnderScore

Senior member
Oct 9, 1999
216
0
0
this solution assumes that you have textfile like so:

192.168.1.51
192.168.1.53
192.168.1.58


it opens it up, loads each line into an array, then closes the file.
processes it and spits out the results.

The gethost~~~ portion was yanked from the Perl doc.

i added the file I/O portion.

#!/usr/bin/perl
use Net::hostent;
use Socket;

print "Enter filename of hosts: ";
$in_file = <STDIN>;
chomp($in_file);
open(INPUT, $in_file) || die &quot;\nSorry, couldn't create open $in_file: $!\n&quot;;
while ( <INPUT> ) {
@MyArr = (@MyArr, $_);
}

close(INPUT) || die &quot;Sorry, couldn't close $in_file: $!&quot;;
foreach $findhost ( @MyArr ) {
unless ($h = gethost($findhost)) {
warn &quot;$0: no such host: $findhost\n&quot;;
next;
}

printf &quot;\n%s is %s%s\n&quot;, $findhost, lc($h->name) eq lc($findhost) ? &quot;&quot; : &quot;*really* &quot;,$h->name;

print &quot;\taliases are &quot;, join(&quot;, &quot;, @{$h->aliases}), &quot;\n&quot;
if @{$h->aliases};

if ( @{$h->addr_list} > 1 ) {
my $i;
for $addr ( @{$h->addr_list} ) {
printf &quot;\taddr #%d is [%s]\n&quot;, $i++, inet_ntoa($addr);
}
} else {
printf &quot;\taddress is [%s]\n&quot;, inet_ntoa($h->addr);
}

if ($h = gethostbyaddr($h->addr)) {
if (lc($h->name) ne lc($findhost)) {
printf &quot;\tThat addr reverses to host %s!\n&quot;, $h->name;
$findhost = $h->name;
redo;
}
}
}



it creates the following output:
192.168.1.51
is *really* mx-internal.mycompany.com
address is [192.168.1.51]
That addr reverses to host mx-internal.mycompany.com!

mx-internal.mycompany.com is mx-internal.mycompany.com
address is [192.168.1.51]

192.168.1.53
is *really* isweb.mycompany.com
address is [192.168.1.53]
That addr reverses to host isweb.mycompany.com!

isweb.mycompany.com is isweb.mycompany.com
address is [192.168.1.53]

192.168.1.58
is *really* opsweb.mycompany.com
address is [192.168.1.58]
That addr reverses to host opsweb.mycompany.com!

opsweb.mycompany.com is opsweb.mycompany.com
address is [192.168.1.58]




<<<< edit >>>> This forum destroyed my perl style/indentation. :confused: