#!/usr/local/bin/ruby # # TO RUN THIS SCRIPT, FIRST INSTALL THE LATEST VERSION OF RUBY (1.8.4 AS OF THIS WRITING) # AND THEN INSTALL RUBYGEMS. IF YOU ARE USING WINDOWS, THE ONE-CLICK INSTALLER COMES # WITH RUBYGEMS INSTALLED READY (http://rubyinstaller.rubyforge.org/). THEN RUN THE # FOLLOWING AS ROOT OR ADMINISTRATOR: # # gem install mechanize ipadmin --include-dependencies # # THEN EDIT THE $directory VARIABLE BELOW WITH YOUR ORGANIZATION'S EXTENSIONS AND EDIT # THE list VARIABLE SO THAT IT CONTAINS THE IP RANGES WHERE YOUR PHONES ARE LOCATED. # THEN RUN # # ruby spa942_directory.rb # # AS ROOT OR ADMINISTRATOR IN THE SAME DIRECTORY WHERE THE ACCOMPANYING icmpping.rb FILE # IS LOCATED. require 'thread' require 'rubygems' require "icmpping" require 'net/http' require 'uri' require_gem 'ipadmin' include Net class String def to_cidr IPAdmin::CIDR.new :CIDR => self end end class Array def to_cidr map{|elem| elem.to_s.to_cidr} end end list = IPAdmin.range(:Boundaries => %w( 10.20.30.1 10.20.30.255 ).to_cidr) list.concat IPAdmin.range(:Boundaries => %w( 10.20.35.1 10.20.36.255 ).to_cidr) list.concat IPAdmin.range(:Boundaries => %w( 10.20.39.1 10.20.39.255 ).to_cidr) def reboot_phone(ipaddr) begin url = URI.parse("http://#{ipaddr}/admin/reboot") req = Net::HTTP::Get.new(url.path) res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)} return true if res.body =~ /SPA will reboot/ rescue return false end false end THREAD_COUNT = 10 mutex = Mutex.new threads = [] pings = 0 attempts = 0 successes = 0 THREAD_COUNT.times do |i| threads << Thread.new do loop do ipaddr = '' stop = false mutex.synchronize do ipaddr = list.shift end stop = true if ipaddr.nil? break if stop mutex.synchronize { pings += 1 } if ICMPPing.ping(ipaddr, 1000) >= 0 mutex.synchronize { attempts += 1 } mutex.synchronize { successes += 1 if reboot_phone(ipaddr) } end mutex.synchronize { print "\r#{pings} hosts pinged, #{attempts} hosts found, #{successes} successful updates..." } end end end threads.each {|t| t.join }