主頁 > 知識庫 > Perl腳本檢測一個域名是否有效

Perl腳本檢測一個域名是否有效

熱門標簽:外呼電信系統(tǒng) 電話機器人哪里有賣 智能機器人電銷神器 萬利達百貨商場地圖標注 熱門電銷機器人 惠州龍門400電話要怎么申請 okcc外呼系統(tǒng)怎么調(diào)速度 上海企業(yè)外呼系統(tǒng) 河南虛擬外呼系統(tǒng)公司

腳本功能:通過ICMP Ping或TCP/SYN探測指定的域名,探測前檢測域名是否有效。

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

測試例子:

# ./check.host.pl 2013.jb51.net
ERR: query 2013.jb51.net failed: NXDOMAIN
2013.jb51.net is DOWN !

# ./check.host.pl www.jb51.net
ACK Reply from www.jb51.net[173.255.214.254] time=307.04 ms
www.jb51.net 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 !

標簽:合肥 淮安 周口 秦皇島 綿陽 百色 綏化 周口

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Perl腳本檢測一個域名是否有效》,本文關(guān)鍵詞  Perl,腳本,檢測,一個,域名,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Perl腳本檢測一個域名是否有效》相關(guān)的同類信息!
  • 本頁收集關(guān)于Perl腳本檢測一個域名是否有效的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章