Add Switch to Print Both Reachable and Unreachable IPs
A new switch -a was added to print both reachable and unreachable hosts. To diffentiate, hosts are now colored based on their status. The -a switch overrides -r and -d.
This commit is contained in:
parent
e45a78b619
commit
991bad27d3
1 changed files with 30 additions and 11 deletions
31
sbin/pinger
31
sbin/pinger
|
@ -6,6 +6,7 @@ use POSIX;
|
||||||
use Socket;
|
use Socket;
|
||||||
use Switch;
|
use Switch;
|
||||||
use Time::HiRes qw/time/;
|
use Time::HiRes qw/time/;
|
||||||
|
use Term::ANSIColor;
|
||||||
|
|
||||||
# force print before buffer is full
|
# force print before buffer is full
|
||||||
$| = 1;
|
$| = 1;
|
||||||
|
@ -14,13 +15,14 @@ $| = 1;
|
||||||
die("pinger must be run as root\n") if($> != 0);
|
die("pinger must be run as root\n") if($> != 0);
|
||||||
|
|
||||||
my $alive = 1;
|
my $alive = 1;
|
||||||
my(%switches, $both, $time, $name, $force, $startTime, $finishTime);
|
my(%switches, $all, $both, $time, $name, $force, $startTime, $finishTime);
|
||||||
|
|
||||||
# handle command line options
|
# handle command line options
|
||||||
my $status = getopts('bdfrnth', \%switches);
|
my $status = getopts('abdfrnth', \%switches);
|
||||||
help() if(!$status || $switches{'h'});
|
help() if(!$status || $switches{'h'});
|
||||||
for (keys %switches) {
|
for (keys %switches) {
|
||||||
switch($_) {
|
switch($_) {
|
||||||
|
case 'a' { $all = 1; }
|
||||||
case 'b' { $both = 1; $name = 1; }
|
case 'b' { $both = 1; $name = 1; }
|
||||||
case 'd' { $alive = 0; }
|
case 'd' { $alive = 0; }
|
||||||
case 'f' { $force = 1; }
|
case 'f' { $force = 1; }
|
||||||
|
@ -36,7 +38,7 @@ die("Usage: pinger START_IP [END_IP]\n")if(scalar(@ARGV) < 1 || scalar(@ARGV) >
|
||||||
|
|
||||||
$startTime = time if($time);
|
$startTime = time if($time);
|
||||||
my $p = Net::Ping->new('icmp');
|
my $p = Net::Ping->new('icmp');
|
||||||
my @ips;
|
my(@reachable_ips,@unreachable_ips);
|
||||||
|
|
||||||
# must be IP addresses
|
# must be IP addresses
|
||||||
if($ARGV[0] !~ /((?:\d{1,3}\.){3}\d{1,3})/ || (defined($ARGV[1]) && $ARGV[1] !~ /((?:\d{1,3}\.){3}\d{1,3})/)) {
|
if($ARGV[0] !~ /((?:\d{1,3}\.){3}\d{1,3})/ || (defined($ARGV[1]) && $ARGV[1] !~ /((?:\d{1,3}\.){3}\d{1,3})/)) {
|
||||||
|
@ -70,7 +72,6 @@ for(my $i = $start, my $k = 1; $i <= $end; $i++, $k++) {
|
||||||
my $ip = $baseIp . '.' . $i;
|
my $ip = $baseIp . '.' . $i;
|
||||||
|
|
||||||
my $reachable = $p->ping($ip, 1);
|
my $reachable = $p->ping($ip, 1);
|
||||||
if($reachable == $alive) {
|
|
||||||
if($name && ($reachable || $force)) {
|
if($name && ($reachable || $force)) {
|
||||||
my $hostaddr = gethostbyaddr(inet_aton($ip), AF_INET);
|
my $hostaddr = gethostbyaddr(inet_aton($ip), AF_INET);
|
||||||
if($hostaddr ne "") {
|
if($hostaddr ne "") {
|
||||||
|
@ -78,14 +79,31 @@ for(my $i = $start, my $k = 1; $i <= $end; $i++, $k++) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
push(@ips, $ip);
|
if($reachable) {
|
||||||
|
push(@reachable_ips, colored($ip, 'green'));
|
||||||
|
} else {
|
||||||
|
push(@unreachable_ips, colored($ip, 'red'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$p->close();
|
$p->close();
|
||||||
$finishTime = time if($time);
|
$finishTime = time if($time);
|
||||||
|
|
||||||
print "\n\n";
|
print "\n\n";
|
||||||
($alive) ? print "Reachable:\n" : print "Dead:\n";
|
my @ips;
|
||||||
|
if($all) {
|
||||||
|
print "All:\n";
|
||||||
|
@ips = (@reachable_ips, @unreachable_ips);
|
||||||
|
@ips = map { $_->[0] }
|
||||||
|
sort { $a->[1] <=> $b->[1] }
|
||||||
|
map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\.+/, $_))] }
|
||||||
|
@ips;
|
||||||
|
} elsif($alive) {
|
||||||
|
print "Reachable:\n";
|
||||||
|
@ips = @reachable_ips;
|
||||||
|
} else {
|
||||||
|
print "Dead:\n";
|
||||||
|
@ips = @unreachable_ips;
|
||||||
|
}
|
||||||
my $delimeter = ($both) ? "\n" : ", ";
|
my $delimeter = ($both) ? "\n" : ", ";
|
||||||
print join($delimeter, @ips);
|
print join($delimeter, @ips);
|
||||||
print "\n\n";
|
print "\n\n";
|
||||||
|
@ -132,6 +150,7 @@ sub progressBar {
|
||||||
|
|
||||||
sub help {
|
sub help {
|
||||||
print "usage:\t pinger [-d] start [finish]\n\n";
|
print "usage:\t pinger [-d] start [finish]\n\n";
|
||||||
|
print "\t-a\t\tList both reachable and unreachable hosts\n";
|
||||||
print "\t-b\t\tList both hostname and ip address\n";
|
print "\t-b\t\tList both hostname and ip address\n";
|
||||||
print "\t-d\t\tList dead addresses\n";
|
print "\t-d\t\tList dead addresses\n";
|
||||||
print "\t-f\t\tForce hostname lookup even if unreachable\n";
|
print "\t-f\t\tForce hostname lookup even if unreachable\n";
|
||||||
|
|
Loading…
Reference in a new issue