1 |
dpavlin |
330 |
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 |
dpavlin |
332 |
use client; |
10 |
dpavlin |
330 |
|
11 |
|
|
sub host { |
12 |
|
|
Net::Ping->new->ping( shift, 0.7 ); |
13 |
|
|
} |
14 |
|
|
|
15 |
|
|
sub fping { |
16 |
|
|
my $p = Net::Ping->new('syn', 0.3); |
17 |
|
|
$p->hires; |
18 |
|
|
|
19 |
|
|
my $status; |
20 |
|
|
my %syn; |
21 |
|
|
|
22 |
|
|
foreach my $host ( @_ ) { |
23 |
|
|
my ($ret,$nslookup_duration,$ip) = $p->ping($host); |
24 |
|
|
|
25 |
|
|
if ( $ret ) { |
26 |
|
|
$syn{$host} = $ip; |
27 |
|
|
# $status->{dns}->{$ip} = $nslookup_duration * 1000; |
28 |
|
|
} else { |
29 |
|
|
push @{ $status->{address_not_found} }, $host; |
30 |
|
|
} |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
while (my ($host,$rtt,$ip) = $p->ack) { |
34 |
|
|
$status->{$ip}->{rtt} = $rtt * 1000; # ms |
35 |
dpavlin |
331 |
warn "# $host $rtt $ip\n"; |
36 |
|
|
client::conf( $ip => 'hostname', default => $host ) if $host ne $ip; |
37 |
dpavlin |
330 |
} |
38 |
|
|
|
39 |
dpavlin |
334 |
warn "# fping ",dump($status); |
40 |
dpavlin |
330 |
|
41 |
|
|
return $status; |
42 |
|
|
|
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
1; |