Forum: Ruby Capturing output from backticks

E9ea32f2666ea4da53e7e53a0de951af?d=identicon&s=25 Matthew Smith (fizgig)
on 2013-09-05 17:52
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?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-05 18:04
My approach (in windows) would be

/time=(?<time>[^ ]+)/ =~ `ping 192.168.1.254 -n 1`
time #=> "1ms"
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-09-05 18:06
(Received via mailing list)
On Thu, Sep 5, 2013 at 5:52 PM, Matthew Smith <lists@ruby-forum.com>
wrote:
> Response = 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
> ping_response = `ping -c1 -W1 #{target} | grep seq`
> --
> 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.
Bbd572d186d7046bc121426e2a38de49?d=identicon&s=25 Tyler (Guest)
on 2013-09-05 19:12
(Received via mailing list)
unsubscribe


On Thu, Sep 5, 2013 at 9:05 AM, Jess Gabriel y Galn <
9e2c9089d8aa2b34c04a7492c4f2ba00?d=identicon&s=25 Tyler (Guest)
on 2013-09-05 19:13
(Received via mailing list)
unsubscribe
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2013-09-05 19:29
(Received via mailing list)
Am 05.09.2013 19:11, schrieb Tyler:
> unsubscribe

You need to send this to

  ruby-talk-request@ruby-lang.org

And please do not hijack threads.

Regards,
Marcus
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-09-06 04:42
(Received via mailing list)
On Sep 5, 2013, at 10:52 AM, 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
>

So let's dissect this a bit.

> 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
>

Since the single packet was lost, and you have the -W1 switch on, it
won't report anything, given:

-W waittime
             Time in milliseconds to wait for a reply for each packet
sent.  If a reply arrives later, the packet is
             not printed as replied, but considered as replied when
calculating statistics.

since the reply never showed up, clearly beyond the 1 millisecond wait
time. Why you did not get a ping response in that time may be a separate
issue.


> ----> 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`

It would likely be better to do this inside ruby[1], but if you really
insist on doing it from the shell, this might be a little better:

ping_response = `ping -c1 -W1 #{target}|sed -n -e 'p2'`

as it will always print whatever your second line is. However, that
still doesn't address why you're not getting the second line.

I greatly suggest removing the -W1 so you can actually see the response
from the ping if it's timing out.

> --
> Posted via http://www.ruby-forum.com/.

[1] inside ruby way:

ping_response = `ping -c1 -W1 #{target}`.split(/\r?\n/)
second_line = ping_response[1]
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.