说明:
The syntax is (name, altnames, addrtype, len, addrs) = gethostbyname (inname);
1,使用perl的gethostbyname方法,从hosts文件中取得主机名。
代码:
#!/usr/local/bin/perl
#edit: www.jb200.com
$machine ="123.1.1.1";
if (!(($name, $altnames, $addrtype, $len, @addrlist) = gethostbyname ($machine))) {
die ("Machine name $machine not found.n");
}
print ("Equivalent addresses:n");
for ($i = 0; $i < @addrlist; $i++) {
@addrbytes = unpack("C4", $addrlist[$i]);
$realaddr = join (".", @addrbytes);
print ("t$realaddrn");
}
2,获取系统主机名