On Thu, Sep 5, 2013 at 5:52 PM, Matthew Smith <lists / ruby-forum.com> wrote:
> Here's my program:
>
> #!/usr/bin/ruby
> target = "192.168.1.1"
> ping_response = `ping -c1 -W1 #{target}`
> puts "Ping response = " + ping_response
>
> When I run it, I get:
>
> <--- Start output
> Response = PING 192.168.1.1 (192.168.1.1 56(84) bytes of data.
>
> --- 192.168.1.1 ping statistics ---
> 1 packets transmitted, 0 received, 100% packet loss, time 0ms
> ----> End output
>
> As you can see, the captured line isn't very helpful.
>
> When I run the ping command normally from the command line I get:
> <--- Start output
> PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
> 64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.055 ms
>
> --- 192.168.1.1 ping statistics ---
> 1 packets transmitted, 1 received, 0% packet loss, time 0ms
> rtt min/avg/max/mdev = 0.055/0.055/0.055/0.000 ms
> ----> End output
>
>
> I'd really like to capture the second line that has the response time in
> it.  I thought I could do so by grepping something that only it has:
>
> ping_response = `ping -c1 -W1 #{target} | grep seq`
>
> Now when I run the program I get:
>
> <--- Start output
> Response =
> ----> End output
>
>
> Anyone know how to get this done?
>
> --
> Posted via http://www.ruby-forum.com/.

I'm not seeing the same as you:

2.0.0p195 :001 > output = `ping -c1 -W1 www.google.com`
 => [snip]
2.0.0p195 :002 > puts "ping response: #{output}"
ping response: PING www.google.com (x.x.x.x) 56(84) bytes of data.
64 bytes from xxxxx (x.x.x.x): icmp_req=1 ttl=54 time=28.3 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 28.335/28.335/28.335/0.000 ms
 => nil
2.0.0p195 :004 > output.split("\n")[1]
 => "64 bytes from xxxxxx (x.x.x.x): icmp_req=1 ttl=54 time=28.3 ms"

So I do get that second line with the time.

Jesus.