#!/usr/bin/perl -w use String::Util 'trim'; =head1 NAME ping_linux.pl - This script works on Linux system pings a host and returns statistics data. =head1 VERSION Version 1.0 =head1 AUTHOR Gaurav Dubey (gaurav.d@ronankiinfotech.com =head1 SYNOPSIS ./ping_linux.pl [-c --> count (Number of echo requests to be sent)] [-i --> interval (Interval in milliseconds between echo requests)] [-s --> packetsize (Size of icmp packet)] [-h --> destinationIP (Ip address of destination host)] [-w --> AllowableTime (Max time in seconds for sending receiving echo requests/reponse)]" =head1 DESCRIPTION This pings a host via the system ping command and returns RTA ,Tx packets,Rx packets,TTL,Packet-loss =cut use strict; use Getopt::Long; use Pod::Usage; my ($host,$count,$interval,$packetsize,$allowableTime); GetOptions( "h|host=s", \$host, "c|count=i", \$count, "i|interval=i", \$interval, "s|packetsize=i", \$packetsize, "w|allowableTime=i",\$allowableTime, ); #pod2usage("$0: No host given!\n") unless($host); pod2usage("$0:No host given!\n") unless($host && $host =~ /^((([2][5][0-5]|([2][0-4]|[1][0-9]|[0-9])?[0-9])\.){3})([2][5][0-5]|([2][0-4]|[1][0-9]|[0-9])?[0-9])$/); $count = 5 unless ($count); $interval = 1 unless ($interval); $packetsize = 56 unless ($packetsize); $allowableTime = 1 unless ($allowableTime); #Here I am opening a ping process on linux system . open CMD, "/bin/ping -c $count -i $interval -s $packetsize -w $allowableTime $host |" or die "Can't open ping: $!"; #This code has been working fine by executing "./ping_linux.pl -h 192.168.1.150 -c 1" from terminal.And I want to mention it that this host is up and running with in our network. my (@values1,@val1,@values2,@val2,@values3,@values4,@values5); while () { #In below, if-block I am extracting the host-ip,Packet Size,Packet+Header Size from the output line #PING 192.168.1.150 (192.168.1.150) 56(84) bytes of data if ( $_ =~ /PING/ ){ @values1 = split ; @val1 = split(/\(/,$values1[3]); $val1[1] =~ s/\)//; } #Below if-block has been used to extract the TTL values from the line "64 bytes from 192.168.1.150: icmp_req=1 ttl=254 time=4.43 ms" #which is 254 if ($. == 2) { @values2 = split; @val2 = split(/\=/ , $values2[5]); } #in this if-block I am extracting the transfer-recieved packets and packet loss ,from the output line "3 packets transmitted, 3 received, 0% packet loss, time 2003ms" if ($_ =~ /packets/) { #print "$_ "; @values3 = split; } #In last if-block I am extracting the rtt avg/max/min/med values ,from the output-line "rtt min/avg/max/mdev = 2.113/2.895/4.434/1.089 ms" if ($_ =~ /^rtt/) { #print "$_ "; @values4 = split /\=/; @values5 = split(/\// ,$values4[1]); } } #print "$values1[1] \n"; # print "$val1[0] \n"; #print "$val1[1] \n"; #print "$val2[1] \n"; #print "$values3[0] \n"; #print "$values3[3] \n"; #print "$values3[5] \n"; #print "$values4[1]"; #my $min = trim($values5[0]); #print "$min\n"; #print "$values5[1] \n"; #print "$values5[2] \n"; #print "$values5[3] "; close CMD