1

我在让 Net::SSH::Multi 库工作时遇到问题,它应该连接到每个盒子并运行该命令,我正在尝试获取输出。

这是我的代码:

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
require 'net/ssh/multi'

@ssh_responses = Array.new
puts "Starting vhost grab..."
# SSH into each instance, and download Vhost Dump
Net::SSH::Multi.start(:on_error => :ignore) do |ssh|

  ssh.use 'ec2-1-1-1-1.compute-1.amazonaws.com', :user => 'portal_user', :keys => ['~/.ssh/portal_user.pem']
  ssh.use 'ec2-1-1-1-2.compute-1.amazonaws.com', :user => 'portal_user', :keys => ['~/.ssh/portal_user.pem']
  puts "Added servers..."
  stdout = ""
  # run this on all boxes
  ssh.exec 'pwd' do |channel, stream, data|
    puts "Trying execution..."
    channel.request_pty do |c, success|
      puts "Trying to request tty..."
      if success
        puts "Successfully requested tty"
        command = "sudo ls /root"
        c.exec(command) do |c, success, data|
          puts "Executing commands..."
          stdout << data if stream == :stdout
        end
        @ssh_responses << stdout
        puts "Storing responses..."
      end
    end
  end

  ssh.loop

end

puts @ssh_responses
puts "Script executed."

我从跑步中得到的回应是:

$ ruby serverAction.rb 
Starting vhost grab...
Added servers...
Trying execution...
Script executed.
4

0 回答 0