Sometimes its useful to have smarter output from a ping command. The things I needed were
- 1 second intervals
- the time the ping happened
- how many pings since the last state change
- the status
I have needed this on several occasions when doing redundancy testing, but it has always been more pressing to do the job, rather than write little scripts, but laziness got the better of me and it was time to make this little task a bit simpler. The result is a very simple ruby script.
#!/usr/bin/ruby host=ARGV[0] ping = `which ping`.chomp args = " -c 1 -t 1 #{host}" cmd = "#{ping} #{args}" puts "#{cmd}" `#{cmd}` state = $?.to_i newstate = state count = 0 next_time=Time.now+1 while ( newstate == state ) count = count + 1 time = Time.now puts "at #{time.min}:#{time.sec}, count since change is: #{count}, and #{host} is: " + ( state.zero? ? "Up" : "Down") `#{cmd}` newstate = $?.to_i if ( newstate != state ) then count = 0 state = newstate end pause = next_time-Time.now if pause > 0 then sleep pause end next_time=Time.now+1 end
The output is quite useful now
at 14:7, count since change is: 1, and 10.1.32.1 is: Up at 14:8, count since change is: 2, and 10.1.32.1 is: Up at 14:9, count since change is: 3, and 10.1.32.1 is: Up at 14:10, count since change is: 4, and 10.1.32.1 is: Up at 14:11, count since change is: 5, and 10.1.32.1 is: Up at 14:12, count since change is: 6, and 10.1.32.1 is: Up at 14:13, count since change is: 7, and 10.1.32.1 is: Up
There are probably loads of little things that could be changed to make it more useful or to provide better output, throw in any suggestions.
Cheers
Pete
Recent Comments