-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathping
executable file
·89 lines (57 loc) · 1.5 KB
/
ping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl
=begin metadata
Name: ping
Description: probe for network hosts
Author: Nick Ing-Simmons, [email protected]
License: perl
=end metadata
=cut
use strict;
use Getopt::Std;
use Socket;
use Net::Ping;
sub usage {
require Pod::Usage;
Pod::Usage::pod2usage({ -exitval => 1, -verbose => 0 });
}
my %opt;
getopts('nI:', \%opt) or usage();
my $host = shift;
my $timeout = shift;
usage() unless defined $host;
$timeout = 20 unless defined $timeout;
usage() if @ARGV;
my $a = gethostbyname($host);
if ( $a ) {
if ( $opt{'n'} ) {
my $name = inet_ntoa($a);
$host = $name if ($name);
} else {
my $name = gethostbyaddr($a,PF_INET);
$host = $name if ($name);
}
my $handle = Net::Ping->new($> ? 'udp' : 'icmp', $timeout);
if ( $handle->ping($host) ) {
warn "$host is alive\n";
} else {
die "No answer from $host";
}
} else {
die "Unknown host $host\n";
}
__END__
=head1 NAME
ping - probe for network hosts
=head1 SYNOPSIS
ping [-n] hostname [ timeout ]
=head1 DESCRIPTION
C<ping> looks up I<hostname> and then attempts to contact it via the network.
If the effective userid permits an ICMP (Internet Control Message Protocol)
ECHO_REQUEST packet is sent, otherwise and attempt is made to connect to
the echo port using UDP protocol.
A I<timeout> may be specified in seconds. The default is 20 seconds.
If C<-n> option is specified then the address of I<hostname> is reported
as numbers.
=head1 AUTHOR
Nick Ing-Simmons <[email protected]>
=cut