Class: FissherBase

Inherits:
Object
  • Object
show all
Defined in:
lib/fissher_base.rb

Instance Method Summary collapse

Instance Method Details

#go_timeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fissher_base.rb', line 10

def go_time
  opts = FissherConf.handle_opts unless !opts.nil?
  abort "No hosts specified! Please use -H or -G!\n" unless !opts[:hostlist].nil?

  Net::SSH::Multi.start(:concurrent_connections => opts[:concurrency]) do |session|
    if opts[:gateway]
      session.via opts[:gateway], opts[:user], :password => opts[:password]
    end

    # Create our connection list
    opts[:hostlist].each do |host|
      session.use host, :user => opts[:user], :password => opts[:password]
    end

    if opts[:command] =~ /^sudo/
      if opts[:password].nil?
        p = FissherConf::Misc.new
        opts[:password] = p.getpass
      end

      # Get a PTY and exec command on servers
      session.open_channel do |ch|
        ch.request_pty do |c, success|
          raise "could not request pty" unless success
          ch.exec opts[:command]
          ch.on_data do |c_, data|
            if data =~ /\[sudo\]/
              ch.send_data(opts[:password] + "\n")
            else
              puts "[#{ch[:host]}]: #{data}" unless data =~ /^\n$|^\s*$/
            end
          end
        end
      end
    else
      # Sudo isn't needed. We don't need a PTY.
      session.exec(opts[:command]) 
    end

    # go time... for real.
    session.loop
  end
end