Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

ping count is automatically setting to 1

by gaurav (Acolyte)
on Aug 21, 2013 at 06:03 UTC ( #1050285=perlquestion: print w/ replies, xml ) Need Help??
gaurav has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks!!. I have one problem in my ping Perl script. I have open one ping process but this process has been taking count value as 1 even-though I have set it as 5

#!/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,$p); 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 = 64 unless ($packetsize); $allowableTime = 1 unless ($allowableTime); #print "\$count is : $count \n"; open CMD, "/bin/ping -c $count -i $interval -s $packetsize -w $allowa +bleTime $host |" or die "Can't open ping: $!"; while (<CMD>) { print "$_ "; } close CMD;

Please do not suggest to use Net::Ping ,I am using system ping command for some reasons. So please help me .Yesterday it was working fine,don't know what happen today. Below I am showing my output

[root@localhost Perl_Ping]# ./sample2.pl -h 192.168.1.150 $count is : 5 PING 192.168.1.150 (192.168.1.150) 64(92) bytes of data. 72 bytes from 192.168.1.150: icmp_req=1 ttl=254 time=11.1 ms --- 192.168.1.150 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 11.150/11.150/11.150/0.000 ms

Thanks,I wan to add 1 thing that through terminal its working fine

[root@localhost Perl_Ping]# ping 192.168.1.150 -c 5 PING 192.168.1.150 (192.168.1.150) 56(84) bytes of data. 64 bytes from 192.168.1.150: icmp_req=1 ttl=254 time=2.12 ms 64 bytes from 192.168.1.150: icmp_req=2 ttl=254 time=2.07 ms 64 bytes from 192.168.1.150: icmp_req=3 ttl=254 time=2.18 ms 64 bytes from 192.168.1.150: icmp_req=4 ttl=254 time=2.13 ms 64 bytes from 192.168.1.150: icmp_req=5 ttl=254 time=2.15 ms --- 192.168.1.150 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4005ms rtt min/avg/max/mdev = 2.072/2.134/2.187/0.055 ms

Comment on ping count is automatically setting to 1
Select or Download Code
Re: ping count is automatically setting to 1
by rnewsham (Hermit) on Aug 21, 2013 at 06:37 UTC

    You are setting the deadline option with -w to 1 second. Which will terminate the ping after one second regardless of how many packets have been transmitted and received. If you increase this default you should get the five results you expect.

    "Please do not suggest to use Net::Ping ,I am using system ping command for some reasons."

    You will getter better responses from the monks if you can actually state your reasons. Using Net::Ping is generally going to be the better option compared to shelling out to system ping. If you want to shell out or reinvent the wheel you need a good reason to do so. Without a reason this comes across as homework.

      Thanks,later I figured it out,accidentally I changed it

      "Please do not suggest to use Net::Ping ,I am using system ping command for some reasons."

      You will getter better responses from the monks if you can actually state your reasons. Using Net::Ping is generally going to be the better option compared to shelling out to system ping. If you want to shell out or reinvent the wheel you need a good reason to do so. Without a reason this comes across as homework.

      I think the reason is that I came off a little mean at around 1050149 when I saw no effort coming from him...
Re: ping count is automatically setting to 1
by zork42 (Monk) on Aug 22, 2013 at 06:23 UTC
    BUG: "use warnings;" is missing.

    Always have both: "use strict; use warnings;"

      Thank you I will keep this thing in my mind

Re: ping count is automatically setting to 1
by Mr. Muskrat (Abbot) on Aug 22, 2013 at 18:49 UTC

    This is based on a combination of your code above, the code from 1050022 and some of my own.

    #!/bin/env perl use strict; use warnings; use Getopt::Long; use Net::Ping; my $host; my $count = 5; my $interval = 1; my $packetsize = 64; # remember that header data will be added to this my $timeout = 5; # 5 is the default in Net::Ping GetOptions( "h|host=s", \$host, "c|count=i", \$count, "i|interval=i", \$interval, "s|packetsize=i", \$packetsize, "W|timeout=i", \$timeout, # you had w|allowableTime but t +his makes more sense to me ); die "$0: No host given!\n" unless ( defined $host ); my $p = Net::Ping->new( 'tcp', # BUG: Should be able to change this with a command line op +tion $timeout, $packetsize, ); $p->hires(); my %stats = ( received => 0, tsum => 0, max => 0, min => 9999999999 ); for ( 1 .. $count ) { my ( $ret, $duration, $ip ) = $p->ping( $host ); if ( $ret ) { $duration *= 1000; $stats{received}++; $stats{min} = $duration if ( $duration < $stats{min} ); $stats{max} = $duration if ( $duration > $stats{max} ); $stats{tsum} += $duration; printf "Your RTT is %.2f ms\n", $duration; } sleep $interval if ( defined $interval && $interval > 0 ); } if ( $stats{received} > 0 ) { $stats{avg} = $stats{tsum} / $stats{received}; } $stats{loss} = ( $count - $stats{received} ) / $count * 100; print "--- $host ping statistics ---\n"; printf "%d packets transmitted, %d received, %d%% packet loss\n", $cou +nt, $stats{received}, $stats{loss}; printf "rtt min/avg/max = %.2f/%.2f/%.2f ms\n", $stats{min}, $stats{av +g}, $stats{max} if $stats{received} > 0; __DATA__ [mmusgrove@nmsdev2 mm-nms_4.5 ~]$ ./ping.pl -h 10.130.25.2 --- 10.130.25.2 ping statistics --- 5 packets transmitted, 0 received, 100% packet loss [mmusgrove@nmsdev2 mm-nms_4.5 ~]$ ./ping.pl -h 10.130.25.1 -c 8 Your RTT is 0.50 ms Your RTT is 0.37 ms Your RTT is 0.34 ms Your RTT is 0.35 ms Your RTT is 0.37 ms Your RTT is 0.36 ms Your RTT is 0.41 ms Your RTT is 0.35 ms --- 10.130.25.1 ping statistics --- 8 packets transmitted, 8 received, 0% packet loss rtt min/avg/max = 0.34/0.38/0.50 ms

      And also RTT MDEV

        Thanks for it,what about TTL value and RTT mdev

      Give a man a fish... and he'll demand it cooked?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050285]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (17)
As of 2014-10-30 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls