gethostbyaddr

gethostbyaddrADDR,ADDRTYPE
This function translates addresses into names (and alternate
addresses). ADDR should be a packed binary
network address, and ADDRTYPE should in
practice usually be AF_INET (from the
Socket module). The return value in list context is:
# 0 1 2 3 4 ...
($name, $aliases, $addrtype, $length, @addrs) =
gethostbyaddr($packed_binary_address, $addrtype);where @addrs is a list of
packed binary 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]);Alternatively, you can convert directly to dot-vector notation
with the v modifier to sprintf:
$dots = sprintf "%vd", $addrs[0];
The inet_ntoa function from the
Socket module is useful for producing a printable version. This
approach will become important if and when we all ever manage to switch
over to IPv6.
use Socket; $printable_address = inet_ntoa($addrs[0]);
In scalar context, gethostbyaddr returns only the
hostname.
To produce an ADDR from a dot vector,
say this:
use Socket;
$ipaddr = inet_aton("127.0.0.1"); # localhost
$claimed_hostname = gethostbyaddr($ipaddr, AF_INET);See the section Sockets in Chapter 15 for more examples. The Net::hostent module supports a by-name interface to this function. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access