Skip to content

Instantly share code, notes, and snippets.

@mikfreedman
Created March 3, 2012 23:32
Show Gist options
  • Save mikfreedman/1968892 to your computer and use it in GitHub Desktop.
Save mikfreedman/1968892 to your computer and use it in GitHub Desktop.
run multiple commands, return interleaved output prefixed with hostname
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