Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I would like to find the IP address' from a list of hostnames. This is how I have approached this, is there a better way without using a system call to nslookup?
if ($machine_name) { my @ip = `nslookup $machine_name`; my $ip = $ip[4]; $ip = (split /: /, $ip, 2)[1]; print "$machine_name:$ip<br>\n"; }
Thank you,

Replies are listed 'Best First'.
Re: How to find IP address from hostname?
by busunsl (Vicar) on Mar 21, 2001 at 21:50 UTC
    Use gethostbyname

    see perldoc -f gethostbyname

Re: How to find IP address from hostname?
by kschwab (Vicar) on Mar 21, 2001 at 22:33 UTC
    perldoc -f gethostbyname is a little confusing, since it gloms together docs for several functions.

    It does, however, point you towards the Socket module (perldoc Socket). The Socket module buys you the handy inet_ntoa() function, as well as other helpful functions and constants.

    Maybe this snippet will help a bit:

    use Socket; $ip = gethostbyname("www.cnn.com"); $string = inet_ntoa($ip); print $string . "\n";
Re: How to find IP address from hostname?
by tomhukins (Curate) on Mar 21, 2001 at 21:58 UTC

    See the Look up a host's addresses section of the documentation for Net::DNS.