For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > printing output of ping command









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author printing output of ping command
TapasranjanMohapatra

2005-02-23, 3:55 pm

Hi All,
I have a script as follows
----------------------------
my $host =3D shift;
my $count =3D shift;
my $result =3D `ping -c $count $host`;
if($result =3D~ m/$count packets transmitted, $count packets received/)
{
$success =3D 1;
}
print "$result\n";
----------------------------

Now, when I run the script, the ping is executed the result is stored in =
the $result variable and printed at the last statement. When the count =
is a big number, I have to wait till the command finishes and then it =
prints at one shot at the last line.
Is it possible to get the output printed as it is seen while running the =
ping command? I mean instead of printing at one shot at the end, I want =
to print line by line as the ping is being executed. Is it possible?

TIA
Tapas

Ankur Gupta

2005-02-23, 3:55 pm

TapasranjanMohapatra wrote:

>Hi All,
>I have a script as follows
>----------------------------
>my $host = shift;
>my $count = shift;
>my $result = `ping -c $count $host`;
>if($result =~ m/$count packets transmitted, $count packets received/)
>{
> $success = 1;
>}
>print "$result\n";
>----------------------------
>
>Now, when I run the script, the ping is executed the result is stored in the $result variable and printed at the last statement. When the count is a big number, I have to wait till the command finishes and then it prints at one shot at the last line.
>Is it possible to get the output printed as it is seen while running the ping command? I mean instead of printing at one shot at the end, I want to print line by line as the ping is being executed. Is it possible?
>
>TIA
>Tapas
>
>
>
>

sure this is possible..
$|=1;
open (PING, "ping -c $count $host |") or die "Cannot execute ping:$!\n";
while(<PING> ){
print;
}



Chris Devers

2005-02-23, 3:55 pm

On Wed, 23 Feb 2005, TapasranjanMohapatra wrote:

> I have a script as follows
> ----------------------------
> my $host = shift;
> my $count = shift;
> my $result = `ping -c $count $host`;
> if($result =~ m/$count packets transmitted, $count packets received/)
> {
> $success = 1;
> }
> print "$result\n";
> ----------------------------


Is there a reason you're using backticks instead of just doing this all
in Perl directly?

<http://search.cpan.org/~bbb/Net-Pin...lib/Net/Ping.pm>

This sidesteps having to make a regex to parse the results, which will
work inconsistently (that is, not at all) with other versions of `ping`.

This approach should be more flexible in general; the documentation at
the URL above should give you the tools you need to do what you want in
a reliable, portable way.


--
Chris Devers
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com