inet_aton() 转换,转换压缩为二进制形式,也可以将主机名转换为压缩的ip地址
gethostbyname 将符号型主机名转换为压缩的ip地址
gethostbyaddr 作相反的转换
inet_ntoa 转换为可阅读的“圆点四元组”
[root@jzlfs perl]# cat gethostbyname.pl
#!/usr/bin/perl -w
use Socket;
while(<>){
chomp;
my $packed_address = gethostbyname($_);
unless ($packed_address){
print "$_ => ?\n";
next
}
my $dotted_quad = inet_ntoa($packed_address);
print "$_ => $dotted_quad \n";
}
测试:
perl gethostbyname.pl < hostname.txt
=> 192.168.1.222
2.将ip转换为主机名
#!/usr/bin/perl -w
use Socket;
my $ADDR_PAT = '^/d \. \d \. \d \.\d $';
while (<>){
chomp;
die "$_: not a valid address" unless /$ADDR_PAT/o;
my $name = gethostbyaddr(inet_aton($_),AF_INET);
$name ||= '?';
print "$_ => $name \n";
}
阅读(2301) | 评论(0) | 转发(0) |