Perl脚本检测一个域名是否有效
来源:易贤网 阅读:800 次 日期:2014-09-25 12:00:01
温馨提示:易贤网小编为您整理了“Perl脚本检测一个域名是否有效”,方便广大网友查阅!

脚本功能:通过ICMP Ping或TCP/SYN探测指定的域名,探测前检测域名是否有效。

?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 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: "; 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"; }

测试例子:


?123456789101112131415 # ./check.host.pl 2013.jb51.net ERR: query 2013.jb51.net failed: NXDOMAIN 2013.jb51.net is DOWN ! # ./check.host.pl ACK Reply from ] time=307.04 ms 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 !

更多信息请查看IT技术专栏

更多信息请查看脚本栏目
易贤网手机网站地址:Perl脚本检测一个域名是否有效
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2025国考·省考课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65099533/13759567129 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标