1 |
package ping; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use Net::Ping; |
7 |
use Data::Dump qw/dump/; |
8 |
use Time::HiRes; |
9 |
|
10 |
sub host { |
11 |
Net::Ping->new->ping( shift, 0.7 ); |
12 |
} |
13 |
|
14 |
sub fping { |
15 |
my $p = Net::Ping->new('syn', 0.3); |
16 |
$p->hires; |
17 |
|
18 |
my $status; |
19 |
my %syn; |
20 |
|
21 |
foreach my $host ( @_ ) { |
22 |
my ($ret,$nslookup_duration,$ip) = $p->ping($host); |
23 |
|
24 |
if ( $ret ) { |
25 |
$syn{$host} = $ip; |
26 |
# $status->{dns}->{$ip} = $nslookup_duration * 1000; |
27 |
} else { |
28 |
push @{ $status->{address_not_found} }, $host; |
29 |
} |
30 |
} |
31 |
|
32 |
while (my ($host,$rtt,$ip) = $p->ack) { |
33 |
$status->{$ip}->{rtt} = $rtt * 1000; # ms |
34 |
warn "# $host $rtt $ip\n"; |
35 |
client::conf( $ip => 'hostname', default => $host ) if $host ne $ip; |
36 |
} |
37 |
|
38 |
warn dump($status); |
39 |
|
40 |
return $status; |
41 |
|
42 |
} |
43 |
|
44 |
1; |