Yuck, please don't use goto. Perl has next and last, which means you nearly never need the evil goto. Also, you could have saved a couple temporary variables in various places; f.ex $RAW = join "", <DATA>; would have let you get away without @INFO. (Even better would have been to write $RAW = do { local $/; <DATA> }; .) print ".."; exit; is nearly always better written as die "..";.
Update { I forgot to mention: you should really check out Mark-Jason Dominus' excellent Program Repair Shop and Red Flags article series on Perl.com }
That said, I would have used Net::Ping (as already suggested), and I'd also use one of the Getopt::* modules to make the script more flexible. Here's one possible rendition:
#!/usr/bin/perl -w
use strict;
use Net::Ping 2.13; # earlier versions do not return ping time
use Getopt::Std;
my %opt = (
i => 1,
m => 65536,
p => "udp",
r => 20,
t => 1,
);
getopts("a:i:m:p:t:r:", \%opt) and $opt{a} or die << "USAGE";
usage: $0 -a addr [-i minsize] [-m maxsize] [-p proto] [-r retr] [-t t
+imeout]
-a destination address
-i initial ping size in bytes [default: 1]
-m max ping size in bytes [default: 63536]
-p protocol: tcp, udp, icmp [default: udp]
-r maximum nr. or retries [default: 20]
-t timeout in seconds [default: 1]
USAGE
my ($addr, $bytes, $maxsize, $proto, $timeout, $retries) =
@opt{qw(a i m p t r)};
$Net::Ping::max_datasize = 65536; # we want to send huge packets
until($bytes > $maxsize) {
my $p = Net::Ping->new($proto, $timeout, $bytes);
$p->hires(1); # precise ping times
my ($success, $time);
my $retry = 0;
($success, $time) = $p->ping($addr)
until $success or (++$retry > $retries);
die "Sorry, host $addr not found or network problem.\n"
if not defined $success;
die "Host $addr seems to have died at $bytes bytes packet size.\n"
if $success == 0;
printf "%3.2f msec for $bytes bytes packet size", $time;
print " (traffic hiccuped, $retry tries)" if $retry > 1;
print "\n";
++$bytes;
};
Update: now tested. I also just noticed that the documentation of Net::Ping says you can only set the data size to 1024 bytes at most. There's a constant for that limit in the module however; I added a line to remove that limit.
Makeshifts last the longest. |