#!/usr/local/bin/ruby -w require 'open3' require 'timeout' require 'thread' mutex = Mutex.new # You should redefine POOL for your own environment. #POOL = /^134\.153\.51\.[56]\d\b/ POOL = /^134\.153\.51\.\d+\b/ SSH = '/usr/bin/ssh -x' UPT = '/usr/bin/uptime' threads = [] uptimes = [] File.new('/etc/hosts').readlines.delete_if { |l| l !~ POOL }.each { |l| ip, name = l.split.values_at(0, 2) threads << Thread.new(ip, name) { |tip, tname| output = "" begin timeout(30) { i, o, e = Open3.popen3("#{SSH} #{tip} #{UPT}") output = o.read output = e.read if output == "" #output.gsub!(/\s+/, " ") #output.gsub!(/^.* up /, "up ") } rescue TimeoutError => err output = "timeout exceeded" end mutex.synchronize { uptimes << "%-12s %s" % [tname, output] } } } puts "Finished loop. Waiting for threads..." threads.each { |t| t.join } puts uptimes.sort