Capturing output from backticks

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?

My approach (in windows) would be

/time=(?[^ ]+)/ =~ ping 192.168.1.254 -n 1
time #=> “1ms”

unsubscribe

On Thu, Sep 5, 2013 at 9:05 AM, Jess Gabriel y Galn <

unsubscribe

Am 05.09.2013 19:11, schrieb Tyler:

unsubscribe

You need to send this to

[email protected]

And please do not hijack threads.

Regards,
Marcus

On Thu, Sep 5, 2013 at 5:52 PM, Matthew S. [email protected]
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.

On Sep 5, 2013, at 10:52 AM, Matthew S. [email protected] 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]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs