2

I am trying to use net-ssh-multi to run a command on a group of servers. For this taks, ssh-key authentication is not an option; a password has to be passed to each server defined in the session.use lines. Here's the problem, 'net/ssh' can take a password parameter, but 'net/ssh/multi' cannot. What I would like to do is somehting like this:

require 'net/ssh'
require 'net/ssh/multi'

#The necessary data is contained in a Ticket object 

my_ticket = Ticket.new

Net::SSH::Multi.start (:password => 'xxxx') do |session|

  # define the servers we want to use

   my_ticket.servers.each do |serv_id|
     session.use "#{my_ticket.user_name}@#{serv_id}"
   end


  # execute commands on all servers
  session.exec "uptime"


  # run the aggregated event loop
  session.loop
end

However, this get me:

file.rb:35:in `start': wrong number of arguments (1 for 2) (ArgumentError) from file.rb:35

I know this is a bit of a n00b question, but I would really appreciate some help.

(http://rubydoc.info/gems/net-ssh-multi/1.1/Net/SSH/Multi)

1 Answer 1

2

Turns out the answer is far simpler than I thought it would be. Poring over the documentation, I noticed this in the Class: Net::SSH::Multi::Server docs:

Class: Net::SSH::Multi::Server

Overview:

Encapsulates the connection information for a single remote server, as well as the Net::SSH session corresponding to that information. You'll rarely need to instantiate one of these directly: instead, you should use Net::SSH::Multi::Session#use.'

So, no class extending or calls to super-classes are necessary. The above can be accomplished with:

require 'net/ssh'
require 'net/ssh/multi'


#The necessary data is contained in a Ticket object 
my_ticket = Ticket.new


Net::SSH::Multi.start do |session|


  # define the servers we want to use
  my_ticket.servers.each do |session_server|
    session.use session_server , :user =>  my_ticket.user_name ,  \
    :password => my_ticket.user_pass
  end


  # execute commands on all servers
  session.exec my_ticket.command_to_do
 
  # run the aggregated event loop
  session.loop
end
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, @AbeVoelker . I hope that was the least embarrassing thing about my code.
Hah sorry, wasn't trying to be fickle there. I'm not competent enough in Ruby yet to comment on the validity of it. But it certainly looks a lot cleaner than the Expect/Tcl scripts I had to use at my previous job to work around us being unable to use SSH keys (couldn't use Ruby or Perl either). :-)
@AbeVoelker No worries, I would blush if I were caught "peaking" instead of "piquing"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.