Created
March 3, 2012 23:32
-
-
Save mikfreedman/1968892 to your computer and use it in GitHub Desktop.
run multiple commands, return interleaved output prefixed with hostname
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open3' | |
hosts = %w{ host1 host2 host3 host4 host5 host6 } | |
commands = [] | |
hosts.each do |host| | |
handles = Open3.popen2e("echo", "#{host}") #replace with ssh call or whathaveyou | |
handles << host | |
handles = [:input, :output, :thread, :host].zip(handles) | |
commands << Hash[handles] | |
end | |
while commands.any? do | |
commands.each do |command| | |
if command[:output].eof | |
commands.delete(command) | |
else | |
puts "#{command[:host]}:#{command[:output].readline}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment