From c0cb572d98708716dc3241e0d73f91143f887b65 Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Fri, 22 Jun 2018 18:45:59 +0300 Subject: remove SSH::connect smell --- lib/oxidized/input/ssh.rb | 59 +++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 28 deletions(-) (limited to 'lib') diff --git a/lib/oxidized/input/ssh.rb b/lib/oxidized/input/ssh.rb index 0bc846f..ef92ef6 100644 --- a/lib/oxidized/input/ssh.rb +++ b/lib/oxidized/input/ssh.rb @@ -21,37 +21,10 @@ module Oxidized @output = '' @pty_options = { term: "vt100" } @node.model.cfg['ssh'].each { |cb| instance_exec(&cb) } - secure = Oxidized.config.input.ssh.secure @log = File.open(Oxidized::Config::Log + "/#{@node.ip}-ssh", 'w') if Oxidized.config.input.debug? - port = vars(:ssh_port) || 22 - - ssh_opts = { - port: port.to_i, - paranoid: secure, - keepalive: true, - password: @node.auth[:password], :timeout => Oxidized.config.timeout, - number_of_password_prompts: 0, - } - - auth_methods = vars(:auth_methods) || %w(none publickey password) - ssh_opts[:auth_methods] = auth_methods - Oxidized.logger.debug "AUTH METHODS::#{auth_methods}" - - if proxy_host = vars(:ssh_proxy) - proxy_command = "ssh " - proxy_command += "-o StrictHostKeyChecking=no " unless secure - proxy_command += "#{proxy_host} -W %h:%p" - proxy = Net::SSH::Proxy::Command.new(proxy_command) - ssh_opts[:proxy] = proxy - end - - ssh_opts[:keys] = [vars(:ssh_keys)].flatten if vars(:ssh_keys) - ssh_opts[:kex] = vars(:ssh_kex).split(/,\s*/) if vars(:ssh_kex) - ssh_opts[:encryption] = vars(:ssh_encryption).split(/,\s*/) if vars(:ssh_encryption) - ssh_opts[:verbose] = Logger::DEBUG if Oxidized.config.input.debug? Oxidized.logger.debug "lib/oxidized/input/ssh.rb: Connecting to #{@node.name}" - @ssh = Net::SSH.start(@node.ip, @node.auth[:username], ssh_opts) + @ssh = Net::SSH.start(@node.ip, @node.auth[:username], get_ssh_opts) unless @exec shell_open @ssh begin @@ -143,5 +116,35 @@ module Oxidized end end end + + def get_ssh_opts + port = vars(:ssh_port) || 22 + ssh_opts = { + port: port.to_i, + paranoid: secure, + keepalive: true, + password: @node.auth[:password], timeout: Oxidized.config.timeout, + number_of_password_prompts: 0 + } + + auth_methods = vars(:auth_methods) || %w(none publickey password) + ssh_opts[:auth_methods] = auth_methods + Oxidized.logger.debug "AUTH METHODS::#{auth_methods}" + + if proxy_host = vars(:ssh_proxy) + proxy_command = "ssh " + proxy_command += "-o StrictHostKeyChecking=no " unless Oxidized.config.input.ssh.secure? + proxy_command += "#{proxy_host} -W %h:%p" + proxy = Net::SSH::Proxy::Command.new(proxy_command) + ssh_opts[:proxy] = proxy + end + + ssh_opts[:keys] = [vars(:ssh_keys)].flatten if vars(:ssh_keys) + ssh_opts[:kex] = vars(:ssh_kex).split(/,\s*/) if vars(:ssh_kex) + ssh_opts[:encryption] = vars(:ssh_encryption).split(/,\s*/) if vars(:ssh_encryption) + ssh_opts[:verbose] = Logger::DEBUG if Oxidized.config.input.debug? + + ssh_opts + end end end -- cgit v1.2.1