Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks , I have a simple question , I am trying to store the result of pinging a server in a variable as follow :
my $output = `ping $host`;
However, when I print the output :
print "ping result $output\n";
it doesn't look like when you run it on a DOS command ,, The result of the script will look somthing like this :
Reply from 10.58.188.11: bytes=32 time=61ms TTL=123 Reply from 10.58.1 +88.11: bytes=32 time=146ms TTL=123 Reply from 10.58.188.11: bytes=32 +time=60ms TTL=123 Reply from 10.58.188.11: bytes=32 time=60ms TTL=123 Ping statistics for 10.58.188.11: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate r +ound trip times in milli-seconds: Minimum = 60ms, Maximum = 146ms, Average = 81ms
I need it to look somthing like :
Reply from 10.58.188.11: bytes=32 time=61ms TTL=123 Reply from 10.58.188.11: bytes=32 time=146ms TTL=123 Reply from 10.58.188.11: bytes=32 time=60ms TTL=123 Reply from 10.58.188.11: bytes=32 time=60ms TTL=123 Ping statistics for 10.58.188.11: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate r +ound trip times in milli-seconds: Minimum = 60ms, Maximum = 146ms, Average = 81ms
Just like the way it appears when ran on the command line , any thoughts ? thanks in advance

Replies are listed 'Best First'.
Re: formatting the output
by ikegami (Patriarch) on Nov 17, 2005 at 21:31 UTC

    I don't see that at all.

    $host = '127.0.0.1'; my $output = `ping $host`; print "ping result $output\n";

    outputs

    ping result Pinging 127.0.0.1 with 32 bytes of data: Reply from 127.0.0.1: bytes=32 time<10ms TTL=128 Reply from 127.0.0.1: bytes=32 time<10ms TTL=128 Reply from 127.0.0.1: bytes=32 time<10ms TTL=128 Reply from 127.0.0.1: bytes=32 time<10ms TTL=128 Ping statistics for 127.0.0.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms

    Is that what you get from that three line program?

    I tried fooling with binmode, $/ and $\ to reproduce your undesirable results to no avail. Are you using cygwin or ActiveState?

    Have you tried using a module like Net::Ping instead?

      I don't get the same result as you , the lines that start with Reply are kind truncated not sure why.
Re: formatting the output
by marto (Cardinal) on Nov 17, 2005 at 21:30 UTC
Re: formatting the output
by jfroebe (Parson) on Nov 17, 2005 at 21:58 UTC

    Hi,

    Looks like there is a chomp or a chop between the ping and the print. Can you post a complete example program? We're not getting the same as you are

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: formatting the output
by GrandFather (Saint) on Nov 17, 2005 at 21:36 UTC
    use strict; my $output = `ping perlmonks.org`; print $output;

    Prints:

    Pinging perlmonks.org [209.197.123.153] with 32 bytes of data: Reply from 209.197.123.153: bytes=32 time=303ms TTL=46 Reply from 209.197.123.153: bytes=32 time=268ms TTL=46 Reply from 209.197.123.153: bytes=32 time=305ms TTL=46 Reply from 209.197.123.153: bytes=32 time=244ms TTL=46 Ping statistics for 209.197.123.153: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 244ms, Maximum = 305ms, Average = 280ms

    for me. There are obvious line end issues, but not of the nature your sample implies.


    DWIM is Perl's answer to Gödel