Last active
August 29, 2015 14:06
-
-
Save tacitochaves/fac9862e8258450ac710 to your computer and use it in GitHub Desktop.
Object of Packet ICMP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use FindBin; | |
use lib "$FindBin::Bin/lib"; | |
use Ping; | |
my $self = Ping->new(); | |
my $loss = $self->getPing('8.8.8.8'); | |
print "Packet $loss" . "% loss\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Ping; | |
use strict; | |
use warnings; | |
sub new { | |
my $class = shift; | |
my $self = { _ip => shift, }; | |
bless $self, $class; | |
return $self; | |
} | |
sub getPing { | |
my ( $self, $dst ) = @_; | |
my $loss; | |
$self->{_ip} = $dst if defined($dst); | |
open( my $fh, "ping -c3 $self->{_ip} |" ) or die "Ping Error!\n"; | |
my @line = (<$fh>); | |
close($fh); | |
foreach my $l (@line) { | |
if ( $l =~ m/(\d+)%/ ) { | |
$loss = "$1"; | |
} | |
} | |
return $loss; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment