0

I have written the following system command to ping a website which gives me desired ICMP response.

response = system("ping -c1 www.stackoverflow.com")

The response is as--

PING stackoverflow.com (64.34.119.12) 56(84) bytes of data.
64 bytes from stackoverflow.com (64.34.119.12): icmp_req=1 ttl=52 time=336 ms

--- stackoverflow.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 336.699/336.699/336.699/0.000 ms
 => true

Now i want to store the average time from the above statistics. So i hit the next system command which is as.

response_time = system("ping -c 1 www.pintile.com | tail -1| awk '{print $4}' | cut -d '/' -f 2")

This gives me the average time but does not store it in the response_time variable. Tha value stored in response_time is true.

335.898
 => true 

Now my question is How to store the average time in the response_time variable??

Swati Aggarwal
  • 1,265
  • 3
  • 19
  • 34

2 Answers2

1

use curl :

curl -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null -s http://www.pintile.com

you will get the output like :

Lookup time:    0.589
Connect time:   0.603
PreXfer time:   0.603
StartXfer time: 1.425

Total time: 2.073

hope that help .

EDITED :

Check this answer.

If you surround your command with backticks, then you don't need to (explicitly) call system() at all.

Use it like :

response_time = `ping -c 1 www.pintile.com | tail -1| awk '{print $4}' | cut -d '/' -f 2`

Now you will get response_time . :)

Community
  • 1
  • 1
swati
  • 1,719
  • 10
  • 13
  • No doubt it gives me the same output but when i run the same using system command in rails console it does not store the value as i mentioned in the question. – Swati Aggarwal May 03 '12 at 07:46
0

Try:

`ping -c1 www.stackoverflow.com` =~ %r[= \d+\.\d+/(\d+\.\d+)]
response_time = $1 and $1.to_f
jdoe
  • 15,665
  • 2
  • 46
  • 48