Skip to content

Instantly share code, notes, and snippets.

@Toggie

Toggie/runcmd.rb Secret

Last active August 29, 2015 14:23
Show Gist options
  • Save Toggie/1dc6f84dbcb5c6a64896 to your computer and use it in GitHub Desktop.
Save Toggie/1dc6f84dbcb5c6a64896 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'pmap'
require 'net/ssh'
# Ghetto script to run a command on hosts provided in a file
# this script will spawn parallel threads to run commands expediently
# to use: ./runcmd.rb hosts.txt user pass "command"
filename = ARGV.shift || raise("no filename provided")
user = ARGV.shift || raise("no user provided")
pass = ARGV.shift || raise("no password provided")
command = ARGV.shift || raise("no command given")
File.open(filename).pmap do |i|
host = i.chomp
begin
Net::SSH.start(host, user, :password => pass, :timeout => 7, :number_of_password_prompts => 0) do|ssh|
result = ssh.exec!(command)
puts "#{host}: #{result}"
end
rescue Timeout::Error
puts "#{host}: !!! Timed out"
rescue Errno::EHOSTUNREACH
puts "#{host}: !!! Host unreachable"
rescue Errno::ECONNREFUSED
puts "#{host}: !!! Connection refused"
rescue Net::SSH::AuthenticationFailed
puts "#{host}: !!! Authentication failure"
rescue Net::SSH::Disconnect
puts "#{host}: !!! Connection closed by remote host"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment