Perl指令碼檢測一個網域名稱是否有效_perl

來源:互聯網
上載者:User

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

file: check.host.pl#!/usr/bin/perluse 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 OKsub 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.netERR: query 2013.jb51.net failed: NXDOMAIN2013.jb51.net is DOWN !# ./check.host.pl www.jb51.netACK Reply from www.jb51.net[173.255.214.254] time=307.04 mswww.jb51.net is online !# ./check.host.pl jb51.net icmpEcho Reply from jb51.net[173.255.214.254] time=205.61 msjb51.net is online !# ./check.host.pl chinagfw.com icmpPING Request for chinagfw.com timed out.chinagfw.com is DOWN !

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.