programming-examples/perl/Network/The gethostaddr function searches the file _etc_hosts for the host name.pl

18 lines
590 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
# The syntax is (name, altnames, addrtype, len, addrs) = gethostbyaddr (inaddr, inaddrtype);
#!/usr/local/bin/perl
$machine ="123.1.1.1";
@bytes = split (/\./, $machine);
$packaddr = pack ("C4", @bytes);
if (!(($name, $altnames, $addrtype, $len, @addrlist) = gethostbyaddr ($packaddr, 2))) {
die ("Address $machine not found.\n");
}
print ("Principal name: $name\n");
if ($altnames ne "") {
print ("Alternative names:\n");
@altlist = split (/\s+/, $altnames);
for ($i = 0; $i < @altlist; $i++) {
print ("\t$altlist[$i]\n");
}
}