Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created November 22, 2016 12:59
Show Gist options
  • Save anonymous/b5a1b0c0ceb415baaa8e64114ed33b07 to your computer and use it in GitHub Desktop.
Save anonymous/b5a1b0c0ceb415baaa8e64114ed33b07 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'net/ssh'
require 'yaml'
require 'colorize'
$conf=YAML::load(File.read('config.yaml'))
class Run_via_ssh
attr_reader :hostname, :username
@@count_hosts_run = 0
@@hosts = Array.new
def initialize(hostname, username=$conf['ssh_user_def'], password=$conf['ssh_pass_def'])
@hostname, @username, @password = hostname, username, password
end
def run_cmd(cmds)
@cmds = cmds
begin
ssh = Net::SSH.start(@hostname, @username, :password => @password)
@cmds.each do |step, commands|
printf "#{step}: ".green
puts "#{commands}"
commands.each do |cmd|
line = '#'*4+'#'*cmd.length
puts line
puts '#'" #{cmd} "'#'
puts line
puts
res = ssh.exec!(cmd)
puts "#{res}".yellow
end
end
ssh.close
@@count_hosts_run += 1
@@hosts << @hostname
rescue
pass_hidden = '*'*@password.length
puts "Unable to connect to #{@hostname} using #{@username}/#{pass_hidden}"
end
end
def self.each_node_run(nodes, cmds)
@nodes = nodes
@nodes.each do |node|
puts "NODE: #{node}".green
Run_via_ssh.new(node).run_cmd(cmds)
puts
end
end
def self.gen_names(subdomains)
pref='fedbst.'
suff='.stageoffice.ru'
subdomains.map{|elem| pref + elem.to_s + suff }
end
def self.print_report
puts "REPORT".blue
puts "*We have created #{@@count_hosts_run} object(s).".green
puts "*We have run commands on the following hosts:".green
@@hosts.uniq!
@@hosts.each {|host| puts " ""#{host}" }
end
end
puts "Running using Run_via_ssh instance".blue
n = Run_via_ssh.new('be1.local', 'yaa', 'MYPASS123456')
n.run_cmd({ "RUNNING ON #{n.hostname} for user #{n.username}": ['echo $HOME']})
Run_via_ssh.print_report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment