1
0
Fork 0

Code Cleanup

Miscellaneous minor functionality fixes, comment additions, and
"refactoring" to make code slightly more readable.
This commit is contained in:
Andrew Tomaka 2013-02-24 00:23:55 -05:00
parent dffd9eef2d
commit d744edf83f

View file

@ -7,16 +7,16 @@ use Socket;
use Switch;
use Time::HiRes qw/time/;
if($> != 0) {
die("pinger must be run as root\n");
}
# force print before buffer is full
$| = 1;
# imcp requires root access
die("pinger must be run as root\n") if($> != 0);
my %switches;
my $alive = 1;
my $time = 0;
my $name = 0;
my($start,$finish);
my(%switches, $time, $name, $startTime, $finishTime);
# handle command line options
my $status = getopts('drnth', \%switches);
help() if(!$status || $switches{'h'});
for (keys %switches) {
@ -29,11 +29,10 @@ for (keys %switches) {
}
}
if(scalar(@ARGV) < 1 || scalar(@ARGV) > 2) {
die("Usage: pinger START_IP [END_IP]\n");
}
$start = time if($time);
die("Usage: pinger START_IP [END_IP]\n")if(scalar(@ARGV) < 1 || scalar(@ARGV) > 2);
$startTime = time if($time);
my $p = Net::Ping->new('icmp');
my @ips;
@ -42,33 +41,35 @@ if($ARGV[0] !~ /((?:\d{1,3}\.){3}\d{1,3})/ || (defined($ARGV[1]) && $ARGV[1] !~
die("Both arguments must be IP addresses if two are specified.\n");
}
# and only the final octet should differ
my @ip1 = split(/\./, $ARGV[0]);
my @ip2;
if(scalar(@ARGV) == 2) {
@ip2 = split(/\./, $ARGV[1]);
} else {
@ip2 = @ip1;
}
# Set the second IP address to the first if no second argument
my @ip2 = (scalar(@ARGV) == 2) ? split(/\./, $ARGV[1]) : @ip1;
# and only the final octet should differ
if($ip1[0] != $ip2[0] || $ip1[1] != $ip2[1] || $ip1[2] != $ip2[2]) {
die("The first three octets of the IP addresses must match.\n")
}
# reverse IPs if 1 is larger than 2
if($ip1[3] > $ip2[3]) {
my $temp = $ip2[3];
$ip2[3] = $ip1[3];
$ip1[3] = $temp;
}
my $baseIp = $ip1[0] . '.' . $ip1[1] . '.' . $ip1[2];
my $start = $ip1[3];
my $end = $ip2[3];
my $count = $ip2[3] - $ip1[3] + 1;
for(my $i = $start, my $k = 1; $i <= $end; $i++, $k++) {
$| = 1;
progressBar($k, $count);
my $ip = $baseIp . '.' . $i;
if($p->ping($ip, 1) == $alive) {
if($name) {
my $reachable = $p->ping($ip, 1);
if($reachable == $alive) {
if($name && $reachable) {
my $hostaddr = gethostbyaddr(inet_aton($ip), AF_INET);
$ip = $hostaddr if($hostaddr ne "");
}
@ -76,17 +77,16 @@ for(my $i = $start, my $k = 1; $i <= $end; $i++, $k++) {
push(@ips, $ip);
}
}
$p->close();
$finish = time if($time);
$finishTime = time if($time);
print "\n";
print "\n\n";
($alive) ? print "Reachable:\n" : print "Dead:\n";
print join(", ", @ips);
print "\n\n";
if($time) {
my $completed = $finish - $start;
my $completed = $finishTime - $startTime;
printf("Completed in %.2f seconds.\n", $completed);
}
@ -97,14 +97,13 @@ sub findTerminalWidth {
require 'sys/ioctl.ph';
$available = 0 unless defined &TIOCGWINSZ;
open(TTY, "+</dev/tty") or $available = 0;
unless (ioctl(TTY, &TIOCGWINSZ, $winsize = '')) {
$available = 0;
}
$available = 0 unless (ioctl(TTY, &TIOCGWINSZ, $winsize = ''));
if($available == 1) {
($row, $col, $xpixel, $ypixel) = unpack('S4', $winsize);
} else {
return -1;
}
close(TTY);
return $col;
}