Well, according to my nslookup, it only takes one host
at a time, not a whole list of them. That said, though,
you could just loop through the IPs and use backticks
to call nslookup on each one:
for my $ip (@ips) {
my $output = `nslookup $ip`;
}
In general, if you need the output of a command, use
backticks; if you don't, use system.
However, in this case, you should probably just use Perl builtins
or the Socket module, if possible. For example, to
translate a host name into an IP address:
use Socket;
my $addr = inet_ntoa scalar gethostbyname $host;
| [reply] [d/l] [select] |
try someting simple like this:
for (@IP){
$resolve=`nslookup $_`;
....additional code using $resolve
{
| [reply] [d/l] |
wow the formatting got screwed up on that reply.
sorry, I guess I need to use html tags to get line breaks?
| [reply] |
| [reply] |
Answering a different question to the one you asked...:-)
If you want to get the 'names' of various hosts from their IP addresses, there is a standard UNIX library call you can use to do this, called 'gethostbyaddr'.
Of course, perl allows you to access this, its listed in the 'perlfunc' part of the perl documentation.
The docs for this are scary, and point you to the wrapper modules Net::hostent.
The docs for these are friendly, and give us:
#!/usr/local/bin/perl -w
use Net::hostent;
my @ips = qw( 1.2.3.4 5.6.7.8 ); # Use your own IPs
my %hosts;
foreach my $ip ( @ips ) {
my $info = gethost( $ip ); # Returns an object
unless( $info ) {
warn( "gethost failed for [$ip] : $! $?" );
next;
}
# ...access other interesting bits of $info if you want...
$hosts{$ip} = $info->name(); # Just want the name
}
# Ta da. The %hosts hash now contains a set of ip => host mappings.
foreach my $ip ( keys %hosts ) {
print "The hostname of IP address [$ip] is [$hosts{$ip}]\n";
}
This doesn't necessarily use the DNS. It depends on how your system is set up (if you are on some types of UNIX you can look at your /etc/nsswitch.conf file to work out what your system will use).
| [reply] [d/l] |