你的分享就是我们的动力 ---﹥

Perl 的 gethostbyaddr 使用方法介绍

时间:2013-05-30 00:12来源:www.chengxuyuans.com 点击:

Perl Socket 客户端获取服务器时间代码

# 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"); 
    } 
} 

Perl代码片段

# Format: gethostbyaddr(ADDRESS, DOMAIN_NUMBER);

#!/bin/perl
$address=pack("C4", 127,0,0,1);
($name, $aliases,  $addrtype, $length, @addrs) = gethostbyaddr($address,2);
($a, $b, $c, $d) = unpack ( 'C4', $addrs[0]);
print "Hostname Is $name and the Internet address Is $a.$b.$c.$d.\n";

转载注明地址:http://www.chengxuyuans.com/Perl/63124.html