Skip to content

Instantly share code, notes, and snippets.

@mhansen
Created April 13, 2010 07:50
Show Gist options
  • Save mhansen/364404 to your computer and use it in GitHub Desktop.
Save mhansen/364404 to your computer and use it in GitHub Desktop.
Quick script, pings a host, if two pings time out in a row, run 'mtr' to pinpoint where the loss is occurring
puts 'Usage: ruby ping.rb IP_TO_MONITOR' unless ARGV[0]
exit unless ARGV[0]
ip = ARGV[0]
while true
ping = IO.popen('ping -q -c2 '+ip)
ping_str = ping.readlines
ping.close
match = ping_str.join.match(/(\d) received/)
num_received = match ? match[1] : '0'
if num_received == '0'
puts Time.now.to_s
mtr = IO.popen('mtr --report -c 10 '+ip)
puts mtr.readlines
mtr.close
puts ''
STDOUT.flush
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment