1
0
Fork 0

Force Hostname Lookup

By default, pinger will not lookup hostnames for IPs that are
unreachable.  This prevents extended times if dead IPs do not have host
entries.  However, there are also cases where DNS resolution is
important even when the IP is unreachable.  pinger now supports this
using the -f flag.
This commit is contained in:
Andrew Tomaka 2013-02-28 07:56:40 -05:00
parent d744edf83f
commit e0e789b58f

View file

@ -14,14 +14,15 @@ $| = 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, $time, $name, $startTime, $finishTime); my(%switches, $time, $name, $force, $startTime, $finishTime);
# handle command line options # handle command line options
my $status = getopts('drnth', \%switches); my $status = getopts('dfrnth', \%switches);
help() if(!$status || $switches{'h'}); help() if(!$status || $switches{'h'});
for (keys %switches) { for (keys %switches) {
switch($_) { switch($_) {
case 'd' { $alive = 0; } case 'd' { $alive = 0; }
case 'f' { $force = 1; }
case 'r' { $alive = 1; } case 'r' { $alive = 1; }
case 'n' { $name = 1; } case 'n' { $name = 1; }
case 't' { $time = 1; } case 't' { $time = 1; }
@ -69,7 +70,7 @@ for(my $i = $start, my $k = 1; $i <= $end; $i++, $k++) {
my $reachable = $p->ping($ip, 1); my $reachable = $p->ping($ip, 1);
if($reachable == $alive) { if($reachable == $alive) {
if($name && $reachable) { if($name && ($reachable || $force)) {
my $hostaddr = gethostbyaddr(inet_aton($ip), AF_INET); my $hostaddr = gethostbyaddr(inet_aton($ip), AF_INET);
$ip = $hostaddr if($hostaddr ne ""); $ip = $hostaddr if($hostaddr ne "");
} }
@ -128,6 +129,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-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-n\t\tReturn list as hostnames when possible\n"; print "\t-n\t\tReturn list as hostnames when possible\n";
print "\t-r\t\tList reachable addresses (default)\n"; print "\t-r\t\tList reachable addresses (default)\n";
print "\t-t\t\tTime ping execution\n"; print "\t-t\t\tTime ping execution\n";