February 2012
Intermediate to advanced
1184 pages
37h 17m
English
gethostbyname NAMEThis function translates a network hostname to its corresponding addresses (and other names). The return value in list context is:
# 0 1 2 3 4 ...
($name, $aliases, $addrtype, $length, @addrs) =
gethostbyname($remote_hostname);where @addrs is a list of raw
addresses. In the Internet domain, each address is (historically) four
bytes long and can be unpacked by saying something like:
($a, $b, $c, $d) = unpack("C4", $addrs[0]);You can convert directly to dot-vector notation with the v modifier to sprintf:
$dots = sprintf "%vd", $addrs[0];
In scalar context, gethostbyname returns only the host
address:
use Socket;
$ipaddr = gethostbyname($remote_host);
printf "%s has address %s\n",
$remote_host, inet_ntoa($ipaddr);See Sockets in Chapter 15
for another approach. The Net::hostent module supports a by-name interface to this function. See
also gethostbyname(3).
Read now
Unlock full access