Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^3: uninitialized values

by Random_Walk (Prior)
on Aug 20, 2013 at 06:30 UTC ( #1050131=note: print w/replies, xml ) Need Help??

in reply to Re^2: uninitialized values
in thread uninitialized values

If you have not looked at it already, you may want to take a look at Net::Ping. that way you avoid a lot of system dependencies. On the down side there are limits to the sort of ping you can perform on Unix, unless you are running as root


Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^4: uninitialized values
by gaurav (Acolyte) on Aug 20, 2013 at 07:26 UTC

    Hi R., I got struck now.I have one problem . My code has been working fine untill the host exist,But suppose if I have given some xyz ip which is not in that network, then its not working.So I want it to tell that Ip is unreachable or any other error msg.Give me some hint

    My code is

    open CMD, "/bin/ping -c $count -i $interval -s $packetsize -w $allowab +leTime $host |" or die "Can't open ping: $!"; #print "$success \n"; my (@values1,@val1,@values2,@val2,@values3,@values4,@values5); while (<CMD>) { if ( $_ =~ /PING/ ){ @values1 = split ; @val1 = split(/\(/,$values1[3]); $val1[1] =~ s/\)//; } if ($. == 2) { @values2 = split; @val2 = split(/\=/ , $values2[5]); } if ($_ =~ /packets/) { #print "$_ "; @values3 = split; } if ($_ =~ /^rtt/) { #print "$_ "; @values4 = split /\=/; @values5 = split(/\// ,$values4[1]); } }

    I hope you help me out of this.....Thanks

      Can you please add comments to your code with examples of what the ping returns in each case. I think you may be looking for several different failure modes:

    • Can not resolve name to IP
    • Can not reach that IP
    • That IP is not responding
    • As different OS/Ping combinations have different results it is hard for us to know what you are looking at in each case. If you add an example of each, in a comment before the related if statement, it will make debugging much easier. Not just for your fellow monks, but also for future maintainers of your code, including your future self.


      Pereant, qui ante nos nostra dixerunt!

        Ok R. Below,I am trying to modify and add comment in my code

        #!/usr/bin/perl -w use String::Util 'trim'; =head1 NAME - This script works on Linux system pings a host and re +turns statistics data. =head1 VERSION Version 1.0 =head1 AUTHOR Gaurav Dubey ( =head1 SYNOPSIS ./ [-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 "./ -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 ( 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 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 -c 1 PING ( 56(84) bytes of data. --- 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 -c 1 PING ( 56(84) bytes of data. From icmp_seq=1 Destination Host Unreachable --- 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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1050131]
[Corion]: Hmmm - and now that I look at it, the gallery I'm using doesn't produce non-Javascript compatible links in the sense that hotlinking to an image will only work for Javascript enabled...
[Corion]: On the other hand, maybe supporting non-Javascript isn't that much a priority, and I'm not exactly sure how I could make it work for both kinds of browsers without server-side magic
[Corion]: Should I ever come across far too much money, this would become my evil maniac lair ;)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-02-27 08:34 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (377 votes). Check out past polls.