本文使用gethostbyaddr函数函数从hosts文件中搜索主机名。
gethostbyaddr函数的格式:
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: $namen");
if ($altnames ne "") {
print ("Alternative names:n");
@altlist = split (/s+/, $altnames);
for ($i = 0; $i < @altlist; $i++) {
print ("t$altlist[$i]n");
}
}