Ok R. Below,I am trying to modify and add comment in my code
#!/usr/bin/perl -w
use String::Util 'trim';
=head1 NAME
ping_linux.pl - This script works on Linux system pings a host and re
+turns 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 ad
+dress of destination host)] [-w --> AllowableTime (Max time in second
+s for sending receiving echo requests/reponse)]"
=head1 DESCRIPTION
This pings a host via the system ping command and returns RTA ,Tx pack
+ets,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 $allowab
+leTime $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 (<CMD>) {
#In below, if-block I am extracting the host-ip,Packet Size,Packet+Hea
+der 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 pa
+cket 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
So its working fine
Now when I passed an IP which is not present in our company network say
ping 192.168.1.10 -c 1
PING 192.168.1.10 (192.168.1.10) 56(84) bytes of data.
--- 192.168.1.10 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
So How can I relay a message by using perl that host is not present in our network?
But if it's present in our company's network but system is down.Say,
#ping 192.168.1.25 -c 1
PING 192.168.1.25 (192.168.1.25) 56(84) bytes of data.
From 192.168.1.23 icmp_seq=1 Destination Host Unreachable
--- 192.168.1.25 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0
+ms
for this I have to say system is down.Remember here one thing that I am using this perl script on Fedora-14 Redhat Linux System
|