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 "\nSorry, couldn't create open $in_file: $!\n";
while ( <INPUT> ) {
@MyArr = (@MyArr, $_);
}
close(INPUT) || die "Sorry, couldn't close $in_file: $!";
foreach $findhost ( @MyArr ) {
unless ($h = gethost($findhost)) {
warn "$0: no such host: $findhost\n";
next;
}
printf "\n%s is %s%s\n", $findhost, lc($h->name) eq lc($findhost) ? "" : "*really* ",$h->name;
print "\taliases are ", join(", ", @{$h->aliases}), "\n"
if @{$h->aliases};
if ( @{$h->addr_list} > 1 ) {
my $i;
for $addr ( @{$h->addr_list} ) {
printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr);
}
} else {
printf "\taddress is [%s]\n", inet_ntoa($h->addr);
}
if ($h = gethostbyaddr($h->addr)) {
if (lc($h->name) ne lc($findhost)) {
printf "\tThat addr reverses to host %s!\n", $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.
