Freitag, 12. März 2010

Mac OS X: ping -f ?

Die Option ping -f (flood) scheint es beim Mac OSX eigenen ping nicht zu geben.
Es gibt zwar adaptiven ping (-a), diese wartet aber im Gegensatz zu flood auf Antworten und sendet nicht einfach munter weiter.
Folgende Lösung in Perl habe ich im MacOSXHints-Forum gefunden:

#!/usr/bin/perl

# v 0.1 by Andrew White
# GPL v3 License applies
# http://www.fsf.org/licensing/licenses/agpl-3.0.html
#
# Usage: adaptiveping.pl [-c count] dest_ip
#
# e.g. adaptiveping.pl -c 100 127.0.0.1
#
# Thanks to DaveE @ Yahoo Answers for perl sub to calc stddev
# http://answers.yahoo.com/question/index?qid=20080516135817AA3KdpX
#
use strict;

my $arg = shift;
my $count = 5; #default # pings
my $dest = "127.0.0.1"; #default IP to ping

if ("$arg" eq "")
{
print "Usage: $0 [-c count] ipaddress\n";
exit(1);
}

exit if ($count < 1); # Duh.

$SIG{'INT'} = 'RESULTS'; # Exit gracefully on CTRL-C

# Crappy argument parsing follows

if ("$arg" eq "-c")
{
$count = shift;
$dest = shift;
}
else
{
$dest = $arg;
}
my $missed = my $min = my $max = 0;
my $junk;
my $ms;
my $start;
my $cum;
my @results = ();
my $counter=0;

print "Pinging $dest $count times\n";
for (my $i = 0; $i < $count; $i++)
{
my $output = `/sbin/ping -o $dest`;
my @lines = split('\n', $output);
(my $result) = grep /bytes from/ ,@lines;
if ("$result" eq "")
{
print "64 bytes from $dest: icmp_seq=1 ttl=54 timed out\n";
$missed ++;
next;
}
$counter = $i + 1;
$result =~ s/icmp_seq=0/icmp_seq=$counter/;
print "$result\n";
($junk,$junk,$junk,$ms) = split('=',$result);
($ms,$junk) = split(' ', $ms);
if (! $start)
{
$start ++;
$min = $ms;
}
$min = $ms if ($ms < $min);
$max = $ms if ($ms > $max);
$cum += $min;
push(@results, $ms);
}
RESULTS();
exit;

sub RESULTS
{
my $lost;
my $avg;
my $received = $#results + 1;
if ($received > 0)
{
$avg = ($cum * 1.0 / $received * 1.0);
$lost = sprintf("%.4f", ($counter - $received) / ($counter * 1.0) * 100);
$avg = sprintf("%.4f", $avg);
} else {
$avg = 'UNDEF';
$lost = 'UNDEF';
}
my $sdev = sprintf("%.4f", standard_deviation(@results));
print "\nNum pings: Sent: $counter Received: $received % loss: $lost\%\n";
print "Minimum: $min ms\n";
print "Maximum: $max ms\n";
print "Mean: $avg ms\n";
print "Standard dev: $sdev ms\n";
exit;
}

sub standard_deviation {
my(@numbers) = @_;
#Prevent division by 0 error in case you get junk data
return undef unless(scalar(@numbers));

# Step 1, find the mean of the numbers
my $total1 = 0;
foreach my $num (@numbers) {
$total1 += $num;
}

my $mean1 = $total1 / (scalar @numbers);

# Step 2, find the mean of the squares of the differences
# between each number and the mean
my $total2 = 0;
my $num;
foreach $num (@numbers) {
$total2 += ($mean1-$num)**2;
}
my $mean2 = $total2 / (scalar @numbers);

# Step 3, standard deviation is the square root of the
# above mean
my $std_dev = sqrt($mean2);
return $std_dev;
}


chmod 755 adaptive_ping.pl
Then run it, e.g.
./adaptive_ping.pl -c 100 www.yahoo.com
Zahl nach belieben erhöhen.

Tipp: Wer chmod nicht vom Terminal sonder per GUI aufrufen will findet in BatChmod ein hübsches grafisches Tool.

Keine Kommentare :

Kommentar veröffentlichen