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?