Perl腳本檢測(cè)一個(gè)域名是否有效
更新時(shí)間:2014年06月20日 10:17:59 投稿:junjie
這篇文章主要介紹了Perl腳本檢測(cè)一個(gè)域名是否有效,檢查域名是否可以正常打開(kāi),需要的朋友可以參考下
腳本功能:通過(guò)ICMP Ping或TCP/SYN探測(cè)指定的域名,探測(cè)前檢測(cè)域名是否有效。
file: check.host.pl
#!/usr/bin/perl
use strict;
use Net::Ping;
use Net::DNS;
use Time::HiRes qw();
$| = 1;
my $DEFAULT_TIMEOUT = 2;
my $PING_TIMEOUT = 2;
my $DNS_TIMEOUT = 3;
### 查詢域名是否有效
sub queryDomain {
my $domain = shift();
my $query = '';
my $dns = Net::DNS::Resolver->new(
tcp_timeout => $DNS_TIMEOUT, udp_timeout => $DNS_TIMEOUT, retry => 1
);
my @nameservers = qw/8.8.8.8 114.114.114.114/;
$dns->nameservers(@nameservers);
eval {
$query = $dns->search($domain,'A');
};
if ($@ or ! $query) {
my $err = $dns->errorstring ;
print "ERR: query $domain failed: $errn";
return if ($err =~ /NXDOMAIN/);
}
return 'OK';
}
### return nothing is FAILED, other is OK
sub pingHost {
my $arg = shift();
return 1 if (ref $arg ne 'HASH');
my $p;
eval { $p = Net::Ping->new($arg->{'proto'},$DEFAULT_TIMEOUT,0) };
if ($@) {
warn "ERR to create Net::Ping object: $@n";
return;
}
$p->hires();
my ($host,$duration,$hip,$rep,$ret);
### tcp/syn ping
if ($arg->{'proto'} eq "syn") {
$p->{port_num} = $arg->{'port'};
$p->ping($arg->{'host'},$PING_TIMEOUT);
if (($host,$duration,$hip) = $p->ack()) {
printf("ACK Reply from $arg->{'host'}[%s] time=%.2f msn", $hip, $duration * 1000);
$ret = 'OK';
} else {
warn "SYN Request for $arg->{'host'} timed out.n";
}
}
### icmp ping
else {
($rep,$duration,$hip) = $p->ping($arg->{'host'},$PING_TIMEOUT);
if ($rep) {
printf("Echo Reply from $arg->{'host'}[%s] time=%.2f msn", $hip, $duration * 1000);
$ret = 'OK';
}
else {
warn "PING Request for $arg->{'host'} timed out.n";
}
}
$p->close;
undef($p);
return $ret;
}
my $ARG = { proto => 'syn', port => 80 };
my $host = $ARGV[0];
my $proto = $ARGV[1];
die "Usage: $0 [icmp]n" if (! $host);
$ARG->{'host'} = $host;
$ARG->{'proto'} = $proto if ($proto);
my $code;
if (&queryDomain($host) eq 'OK' and $code = &pingHost($ARG)) {
print "$host is online !n";
}
else {
print "$host is DOWN !n";
}
測(cè)試?yán)樱?br />
# ./check.host.pl 2013.jb51.net ERR: query 2013.jb51.net failed: NXDOMAIN 2013.jb51.net is DOWN ! # ./check.host.pl www.dhdzp.com ACK Reply from www.dhdzp.com[173.255.214.254] time=307.04 ms www.dhdzp.com is online ! # ./check.host.pl jb51.net icmp Echo Reply from jb51.net[173.255.214.254] time=205.61 ms jb51.net is online ! # ./check.host.pl chinagfw.com icmp PING Request for chinagfw.com timed out. chinagfw.com is DOWN !
相關(guān)文章
Perl刪除前導(dǎo)和拖尾空白(刪除左右空格、空白字符)
這篇文章主要介紹了Perl刪除前導(dǎo)和拖尾空白(刪除左右空格、空白字符),本文給出了多個(gè)方法實(shí)現(xiàn)解決這個(gè)需求,需要的朋友可以參考下2015-06-06
在EditPlus中配置Perl開(kāi)發(fā)編譯環(huán)境
這篇文章主要介紹了在EditPlus中配置Perl開(kāi)發(fā)編譯環(huán)境,配置起來(lái)蠻簡(jiǎn)單,適合小型開(kāi)發(fā)環(huán)境,需要的朋友可以參考下2015-06-06
perl pop push shift unshift實(shí)例介紹
perl的pop跟push操作數(shù)組的最右邊,shift跟unshift操作數(shù)組的最左邊2013-02-02
讓apache2以cgi方式運(yùn)行perl cgi程序的實(shí)現(xiàn)方法
讓apache2以cgi方式運(yùn)行perl cgi程序的方法,供大家學(xué)習(xí)參考2013-02-02

