host2ip.pl

Translate a hostname to the corresponding IP address

#!/usr/bin/perl -w
# host2ip.pl translates a hostname to the corresponding
# IP address. The subroutine host2ip() is invoked with a
# hostname as the only mandatory argument and returns the
# corresponding IP address.
#
# Copyright 2003, Ramiro Gómez.
#
# This program is free software; you can redistribute
# it and/or modify it under the same terms as Perl itself.
use strict;
use Socket;

# die if no argument was given
die "Usage: $0 hostname\n" unless @ARGV;

my $ip = host2ip( shift );
print $ip, "\n";

sub host2ip {
my $host = shift;
my $packed_addr = gethostbyname( $host );

die "Hostname $host doesn't exist.\n" unless $packed_addr;

return inet_ntoa( $packed_addr );
}