Skip to content

Instantly share code, notes, and snippets.

@tacitochaves
Last active August 29, 2015 14:06
Show Gist options
  • Save tacitochaves/fac9862e8258450ac710 to your computer and use it in GitHub Desktop.
Save tacitochaves/fac9862e8258450ac710 to your computer and use it in GitHub Desktop.
Object of Packet ICMP
#!/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";
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