Use Getopts::Std For Command Option Parsing
This commit is contained in:
parent
a88c923a5d
commit
dffd9eef2d
1 changed files with 12 additions and 20 deletions
32
sbin/pinger
32
sbin/pinger
|
@ -1,39 +1,31 @@
|
||||||
#!/usr/bin/env perl
|
#!/usr/bin/env perl
|
||||||
use strict;
|
use strict;
|
||||||
|
use Getopt::Std;
|
||||||
use Net::Ping;
|
use Net::Ping;
|
||||||
use POSIX;
|
use POSIX;
|
||||||
use Socket;
|
use Socket;
|
||||||
|
use Switch;
|
||||||
use Time::HiRes qw/time/;
|
use Time::HiRes qw/time/;
|
||||||
|
|
||||||
if($> != 0) {
|
if($> != 0) {
|
||||||
die("pinger must be run as root\n");
|
die("pinger must be run as root\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
my %switches;
|
||||||
my $alive = 1;
|
my $alive = 1;
|
||||||
my $time = 0;
|
my $time = 0;
|
||||||
my $name = 0;
|
my $name = 0;
|
||||||
my($start,$finish);
|
my($start,$finish);
|
||||||
while($ARGV[0] =~ /\-/) {
|
|
||||||
my $arg = shift(@ARGV);
|
|
||||||
$arg =~ s/-//;
|
|
||||||
|
|
||||||
my @switches = split('', $arg);
|
my $status = getopts('drnth', \%switches);
|
||||||
|
help() if(!$status || $switches{'h'});
|
||||||
foreach my $switch (@switches) {
|
for (keys %switches) {
|
||||||
if($switch eq "d") {
|
switch($_) {
|
||||||
$alive = 0;
|
case 'd' { $alive = 0; }
|
||||||
} elsif($switch eq "r") {
|
case 'r' { $alive = 1; }
|
||||||
$alive = 1;
|
case 'n' { $name = 1; }
|
||||||
} elsif($switch eq "n") {
|
case 't' { $time = 1; }
|
||||||
$name = 1;
|
else {}
|
||||||
} elsif($switch eq "t") {
|
|
||||||
$time = 1;
|
|
||||||
} elsif($switch eq "h") {
|
|
||||||
help();
|
|
||||||
} else {
|
|
||||||
print "pinger: invalid option -- '$switch'\n";
|
|
||||||
help();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue