Class: MyAxes
Instance Method Summary collapse
- #exec(session, cmd, name) ⇒ Object
-
#initialize(config = '~/.myaxes') ⇒ MyAxes
constructor
A new instance of MyAxes.
- #start ⇒ Object
- #use_gw? ⇒ Boolean
- #via_gw ⇒ Object
Constructor Details
#initialize(config = '~/.myaxes') ⇒ MyAxes
Returns a new instance of MyAxes.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/myaxes.rb', line 12 def initialize(config='~/.myaxes') @logger = Logger.new('MyAxes') @logger.outputters = Outputter.stdout @options = Options.new.parse @logger.debug "test #{@options}" if $DEBUG @conf = AxeConfig.new(@options) @config = @conf.read(config) @targets = @conf.targets @threads = [] @ssh_options ={ :port => @config['Global']['ssh_port'], :verbose => @config['Global']['debug_level'].to_sym, :auth_methods => %w(publickey password keyboard-interactive), :keys => @config['Global']['ssh_keys'], :password => @config['Global']['password'] } @commands_proc = Proc.new { |session, hostname| @targets[hostname].each do |query| @logger.debug "Query: #{query}" if $DEBUG name = hostname.chomp.split(".")[0] cmd = "mysql -u #{@config['Targets'][name]['login']} -e '#{query}' -p" output = self.exec(session,cmd,name) puts "\033[0;32m[*] #{hostname}\033[0m: #{output}" end } end |
Instance Method Details
#exec(session, cmd, name) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/myaxes.rb', line 111 def exec(session,cmd,name) channel = session.open_channel do |channel| channel.request_pty do |ch, success| raise "Could not obtain pty (i.e. an interactive ssh session)" if !success end channel.exec(cmd) do |ch, success| die "could not execute command" unless success channel.on_data do |ch, data| if data == "Enter password: " @logger.debug "DEBUG: Password request" if $DEBUG channel.send_data "#{@config['Targets'][name]['password']}\n" else channel[:result] ||= "" channel[:result] << data end end channel.on_extended_data do |ch, type, data| raise "SSH command returned on stderr: #{data}" end end end # Nothing has actually happened yet. Everything above will respond to the # server after each execution of the ssh loop until it has nothing left # to process. For example, if the above recieved a password challenge from # the server, ssh's exec loop would execute twice - once for the password, # then again after clearing the password (or twice more and exit if the # password was bad) channel.wait return channel[:result] # it returns with \r\n at the end end |
#start ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/myaxes.rb', line 43 def start @targets.each_key do |hostname| if self.use_gw? @threads << Thread.new { self.via_gw do |jump_server| begin jump_server.ssh(hostname, @config['Global']['login'], @ssh_options) do |session| @commands_proc.call(session,hostname) end rescue Net::SSH::Disconnect => errmsg warn "#{hostname} : #{errmsg}" rescue Net::SSH::AuthenticationFailed => errmsg warn "#{hostname} : #{errmsg}" rescue Errno::ETIMEDOUT => errmsg warn "#{hostname} : #{errmsg}" rescue Errno::ECONNREFUSED => errmsg warn "#{hostname} : #{errmsg}" end end } else @threads << Thread.new { begin Net::SSH.start(hostname, @config['Global']['login'], @ssh_options) do |session| @commands_proc.call(session,hostname) end rescue Net::SSH::Disconnect => errmsg warn "#{hostname} : #{errmsg}" rescue Net::SSH::AuthenticationFailed => errmsg warn "#{hostname} : #{errmsg}" rescue Errno::ETIMEDOUT => errmsg warn "#{hostname} : #{errmsg}" rescue Errno::ECONNREFUSED => errmsg warn "#{hostname} : #{errmsg}" end } end end @threads.each {|thread| thread.join } end |
#use_gw? ⇒ Boolean
107 108 109 |
# File 'lib/myaxes.rb', line 107 def use_gw? @config['Global']['use_jump'] end |
#via_gw ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/myaxes.rb', line 87 def via_gw begin jump_server = Net::SSH::Gateway.new(@config['Global']['jump_server'], @config['Global']['login'], @ssh_options) @logger.debug "port forwarding ok" if $DEBUG yield jump_server rescue Net::SSH::Disconnect => errmsg warn "Gateway : #{errmsg}" rescue Net::SSH::AuthenticationFailed => errmsg warn "Gateway : #{errmsg}" rescue Errno::ETIMEDOUT => errmsg warn "Gateway : #{errmsg}" rescue Errno::ECONNREFUSED => errmsg warn "Gateway : #{errmsg}" end end |