Title / Description
Code #!/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"
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code