SSH Automation for Windows with Ruby

I have 13 window servers running Jenkins Slaves. For some reason (Windows updates?), The Jenkins Slaves periodically stop working, and the Slave Jenkins service needs to be restarted. If I manually SSH for machines (the cygwin ssh server is running), I simply print:

net stop "Jenkins Slave" net start "Jenkins Slave" 

and that (almost) always solves the problem.

So, I wrote a Ruby script to automate this.

That's what:

 #!/usr/bin/env ruby require 'rubygems' require 'net/ssh' USER = 'Administrator' PASS = 'PASSWORD' hosts = [:breckenridge, :carbondale, :crestone, :denali, :gunnison, :sneffels, "mammoth", "whitney", "snowmass", "firestone", "avon", :grizzly, :silverton] hosts.each {|host| puts "SSHing #{host} ..." Net::SSH.start( HOST, USER, :password => PASS ) do |ssh| puts ssh.exec!('net stop "Jenkins Slave"') puts ssh.exec!('net start "Jenkins Slave"') puts "Logging out..." end } 

the script runs on all machines, I see the output that the service started. However, this never works. When I get back to the car, the service did not start.

Unfortunately, I cannot use Linux - I do not control these machines.

Any ideas on why SSH works manually but the script doesn't work?

Thanks phil

+8
windows ruby ssh jenkins
source share
1 answer

I tried this in Pry and found two questions:

  • HOST undefined, it must be host , since this is a variable passed to the block.
  • SSH.start expects the parameters to be STRING , so add .to_s as follows.

In addition, I switched it to the Ruby idiomatic template using do...end when the block goes through 1 line.

 hosts.each do |host| puts "SSHing #{host} ..." Net::SSH.start( host.to_s, USER, :password => PASS ) do |ssh| puts ssh.exec!('date') puts "Logging out..." end end 

I tested this in Pry and now it works. Hope this helps.

+2
source share

All Articles