parsed ping

Ruby code posted by ericnils
created at 14 Sep 00:03, updated at 22 Oct 14:49

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/ruby1.9.1
beginning = Time.now

#ping -c <number of pings to send> -W <seconds to wait for response>
ping_result = `ping -c 1 -W 1 #{ARGV[0]}`

if $?.success?
#  puts "Ping was successful"
else
  puts "Ping was not successful"
end

ping_result.each_line do |line| 
  if line =~ /bytes from/ 
    split_on_space = line.split " "
    # example line with labeled split_on_space array indexes:
    #|0 |1    |2   |3          |4         |5      |6        |7
    # 64 bytes from 10.1.1.125: icmp_seq=1 ttl=125 time=52.2 ms
    if split_on_space[1] == "bytes" then bytes = split_on_space[0].to_i end
    if split_on_space[6].split("=")[0] == "time" then time_ms = split_on_space[6].split("=")[1].to_f end
    puts "#{bytes} bytes transmitted in #{time_ms} ms"
  end
end

puts "Time elapsed: #{(Time.now - beginning) * 1000} ms"
855 Bytes in 5 ms with coderay