国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Perl > 正文

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

2020-10-31 15:16:33
字體:
來源:轉載
供稿:網友

腳本功能:通過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.VeVB.COmERR: query 2013.VeVB.COm failed: NXDOMAIN2013.VeVB.COm is DOWN !# ./check.host.pl m.survivalescaperooms.comACK Reply from m.survivalescaperooms.com[173.255.214.254] time=307.04 msm.survivalescaperooms.com is online !# ./check.host.pl VeVB.COm icmpEcho Reply from VeVB.COm[173.255.214.254] time=205.61 msVeVB.COm is online !# ./check.host.pl chinagfw.com icmpPING Request for chinagfw.com timed out.chinagfw.com is DOWN !

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 壤塘县| 鲁甸县| 武陟县| 贡山| 缙云县| 朝阳县| 福安市| 额尔古纳市| 施甸县| 井冈山市| 沧州市| 社旗县| 辽源市| 东至县| 孝昌县| 弥勒县| 荥经县| 麻栗坡县| 大理市| 惠来县| 邢台市| 吴旗县| 鄄城县| 曲松县| 江达县| 卓尼县| 延寿县| 沂南县| 米林县| 珠海市| 大方县| 泽库县| 昂仁县| 嘉定区| 固阳县| 专栏| 敦化市| 麻栗坡县| 清镇市| 丘北县| 犍为县|