From e64312db7ee50d9bb20653b0e6e412e87adae6d4 Mon Sep 17 00:00:00 2001 From: Torsten Date: Sat, 26 Mar 2022 17:38:33 +0200 Subject: [PATCH] working version --- lib/routers.rb | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/routers.rb b/lib/routers.rb index 4a8096c..3d82281 100644 --- a/lib/routers.rb +++ b/lib/routers.rb @@ -4,7 +4,7 @@ require "net/ssh" class Routers @@start_address = "10.20.20." - @@start_at = 30 + @@start_at = 43 @@stop_at = 80 def initialize @@ -21,32 +21,50 @@ class Routers # run command end + # takes a block and yields ip and connectio to + # every succesful connection def all_ips - connections = {} while(@at < @@stop_at) - error = nil begin connection = Net::SSH.start(at , "root" , password: Rails.application.credentials.sala, non_interactive: true , timeout: 4) puts "Ok #{at}" - connections[at] = connection + yield( at , connection) + break rescue Net::SSH::ConnectionTimeout - error = " Timed out ssh" + puts "Not at #{at} Timed out ssh" rescue Errno::ETIMEDOUT - error = " Timed out erro" + puts "Not at #{at} Timed out erro" rescue Errno::EHOSTUNREACH - error = " Host unreachable" + puts "Not at #{at} Host unreachable" rescue Errno::ECONNREFUSED - error = " Connection refused" + puts "Not at #{at} Connection refused" rescue Net::SSH::AuthenticationFailed - error = " Authentication failure" + puts "Not at #{at} Authentication failure" end - puts "not at #{at} #{error}" if error @at += 1 end end + # create a reboot at 2:30 + # destructively in the same location, + # ie can rerun to ensure, but other changes will be lost + def make_cron + # Reboot at 2:30am every day + # Note: To avoid infinite reboot loop, wait 70 seconds + # and touch a file in /etc so clock will be set + # properly to 2:31 on reboot before cron starts. + cron_job = "30 2 * * * sleep 70 && touch /etc/banner && reboot" + cron_file = "/etc/crontabs/root" + cron_create = "echo '#{cron_job}' > #{cron_file}" + crond_restart = "/etc/init.d/cron restart" + all_ips do |ip , connection| + connection.exec!(cron_create) + connection.exec!(crond_restart) + puts "Cron for #{ip} ok" + end + end end -Routers.new.all_ips +Routers.new.make_cron