perl腳本檢測一個域名是否有效
來源:易賢網(wǎng) 閱讀:839 次 日期:2016-06-14 09:57:44
溫馨提示:易賢網(wǎng)小編為您整理了“perl腳本檢測一個域名是否有效”,方便廣大網(wǎng)友查閱!

腳本功能:通過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 ! 

更多信息請查看腳本欄目
易賢網(wǎng)手機網(wǎng)站地址:perl腳本檢測一個域名是否有效

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)