Class: Oxidized::SSHWrapper
- Inherits:
-
Object
- Object
- Oxidized::SSHWrapper
- Defined in:
- lib/oxidized/sshwrapper.rb,
lib/oxidized/ssh/version.rb
Constant Summary collapse
- VERSION =
"0.2"
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#exec(params, expectation) ⇒ Object
readonly
Returns the value of attribute exec.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
- #login ⇒ Object
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#password_prompt ⇒ Object
writeonly
Sets the attribute password_prompt.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
-
#pty_options ⇒ Object
writeonly
Sets the attribute pty_options.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
-
#username_prompt ⇒ Object
writeonly
Sets the attribute username_prompt.
Instance Method Summary collapse
- #check_for_connection ⇒ Object
- #collect_output(params, expectation) ⇒ Object
- #connected? ⇒ Boolean
- #create_session ⇒ Object
- #disconnect ⇒ Object
- #exec!(params, expect = @prompt) ⇒ Object
- #expect(*regexps) ⇒ Object
- #expectation_list_handler(data) ⇒ Object
-
#initialize(options) ⇒ SSHWrapper
constructor
A new instance of SSHWrapper.
- #prep_connection ⇒ Object
- #prep_proxy(proxy) ⇒ Object
- #request_channels(ch) ⇒ Object
- #reset_output_buffer ⇒ Object
- #sanitize_output_buffer(sub, *regexs) ⇒ Object
- #send(params) ⇒ Object
- #send_data(params, expectation) ⇒ Object
- #set_data_hook(ch) ⇒ Object
- #setup_channels(ch) ⇒ Object
- #start ⇒ Object
- #start_channel_requests ⇒ Object
Constructor Details
#initialize(options) ⇒ SSHWrapper
Returns a new instance of SSHWrapper.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/oxidized/sshwrapper.rb', line 17 def initialize() @ip = [:ip] @username = [:username] @password = [:password] @verbose = [:verbose] @debug = [:debug] @prompt = [:prompt] @exec = [:exec] || false @pty_options = [:pty_options] ||= { term: "vt100" } @port = [:port] ||= 22 @output = String.new @logger = [:logger] ||= Logger.new(STDOUT) @auth_methods = [:auth_methods] ||= ["publickey", "hostbased", "password"] @expectation_handler = [:expectation_handler] @proxy = prep_proxy([:proxy]) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
10 11 12 |
# File 'lib/oxidized/sshwrapper.rb', line 10 def connection @connection end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
11 12 13 |
# File 'lib/oxidized/sshwrapper.rb', line 11 def debug @debug end |
#exec(params, expectation) ⇒ Object (readonly)
Returns the value of attribute exec.
11 12 13 |
# File 'lib/oxidized/sshwrapper.rb', line 11 def exec @exec end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
10 11 12 |
# File 'lib/oxidized/sshwrapper.rb', line 10 def ip @ip end |
#login ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/oxidized/sshwrapper.rb', line 108 def login if @login match = expect @username_prompt, @prompt if match == @login_prompt exec @username, @password_prompt exec @password end else expect @prompt end end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
12 13 14 |
# File 'lib/oxidized/sshwrapper.rb', line 12 def output @output end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
10 11 12 |
# File 'lib/oxidized/sshwrapper.rb', line 10 def password @password end |
#password_prompt=(value) ⇒ Object (writeonly)
Sets the attribute password_prompt
14 15 16 |
# File 'lib/oxidized/sshwrapper.rb', line 14 def password_prompt=(value) @password_prompt = value end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
12 13 14 |
# File 'lib/oxidized/sshwrapper.rb', line 12 def port @port end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
11 12 13 |
# File 'lib/oxidized/sshwrapper.rb', line 11 def prompt @prompt end |
#pty_options=(value) ⇒ Object (writeonly)
Sets the attribute pty_options
15 16 17 |
# File 'lib/oxidized/sshwrapper.rb', line 15 def (value) @pty_options = value end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
12 13 14 |
# File 'lib/oxidized/sshwrapper.rb', line 12 def session @session end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
10 11 12 |
# File 'lib/oxidized/sshwrapper.rb', line 10 def username @username end |
#username_prompt=(value) ⇒ Object (writeonly)
Sets the attribute username_prompt
14 15 16 |
# File 'lib/oxidized/sshwrapper.rb', line 14 def username_prompt=(value) @username_prompt = value end |
Instance Method Details
#check_for_connection ⇒ Object
59 60 61 |
# File 'lib/oxidized/sshwrapper.rb', line 59 def check_for_connection prep_connection unless @session end |
#collect_output(params, expectation) ⇒ Object
73 74 75 76 |
# File 'lib/oxidized/sshwrapper.rb', line 73 def collect_output(params, expectation) send_data((params + "\n"), expectation) return @output end |
#connected? ⇒ Boolean
176 177 178 |
# File 'lib/oxidized/sshwrapper.rb', line 176 def connected? @connection.closed? end |
#create_session ⇒ Object
125 126 127 128 129 |
# File 'lib/oxidized/sshwrapper.rb', line 125 def create_session @session = @connection.open_channel do |channel| setup_channels(channel) end end |
#disconnect ⇒ Object
169 170 171 172 173 174 |
# File 'lib/oxidized/sshwrapper.rb', line 169 def disconnect Timeout::timeout(5) { @connection.loop } rescue Errno::ECONNRESET, Net::SSH::Disconnect, IOError ensure (@connection.close rescue true) unless @connection.closed? end |
#exec!(params, expect = @prompt) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/oxidized/sshwrapper.rb', line 50 def exec!(params, expect = @prompt) check_for_connection exec(params, expect) sanitize_output_buffer("\n", /\r\n/) sanitize_output_buffer('', params) @logger.debug params if @debug @output end |
#expect(*regexps) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/oxidized/sshwrapper.rb', line 90 def expect *regexps regexps = [regexps].flatten @logger.debug "expecting #{regexps.inspect} at #{@ip}" if @debug @connection.loop(0.1) do @logger.debug @output if @debug sleep 0.1 match = regexps.find { |regexp| @output.match regexp } return match if match true end end |
#expectation_list_handler(data) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/oxidized/sshwrapper.rb', line 144 def expectation_list_handler(data) @expectation_handler.each_slice(2) do |handler, meth| handler.method(meth.to_sym).call(self, data) end @output end |
#prep_connection ⇒ Object
102 103 104 105 106 |
# File 'lib/oxidized/sshwrapper.rb', line 102 def prep_connection return true if @exec start_channel_requests login end |
#prep_proxy(proxy) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/oxidized/sshwrapper.rb', line 34 def prep_proxy(proxy) if proxy_host = proxy proxy_command = "ssh " proxy_command += "-o StrictHostKeyChecking=no " #unless secure proxy_command += "#{proxy_host} -W %h:%p" return Net::SSH::Proxy::Command.new(proxy_command) end end |
#request_channels(ch) ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/oxidized/sshwrapper.rb', line 151 def request_channels(ch) ch.request_pty @pty_options do |_ch, success_pty| raise NoShell, "Can't get PTY" unless success_pty ch.send_channel_request 'shell' do |_ch, success_shell| raise NoShell, "Can't get shell" unless success_shell end end end |
#reset_output_buffer ⇒ Object
160 161 162 |
# File 'lib/oxidized/sshwrapper.rb', line 160 def reset_output_buffer @output = '' end |
#sanitize_output_buffer(sub, *regexs) ⇒ Object
164 165 166 167 |
# File 'lib/oxidized/sshwrapper.rb', line 164 def sanitize_output_buffer sub, *regexs @logger.debug "sanitizing #{regexs.join("|")} with #{sub}" if @debug @output.gsub!(/#{regexs.join("|")}/, sub) end |
#send(params) ⇒ Object
86 87 88 |
# File 'lib/oxidized/sshwrapper.rb', line 86 def send(params) @session.send_data params end |
#send_data(params, expectation) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/oxidized/sshwrapper.rb', line 78 def send_data(params, expectation) reset_output_buffer send(params) @session.process expect expectation if expectation @output end |
#set_data_hook(ch) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/oxidized/sshwrapper.rb', line 136 def set_data_hook(ch) ch.on_data do |_ch, data| #@logger.debug "received #{data}" if @debug @output << data @output = expectation_list_handler(@output) if @expectation_handler end end |
#setup_channels(ch) ⇒ Object
131 132 133 134 |
# File 'lib/oxidized/sshwrapper.rb', line 131 def setup_channels(ch) set_data_hook(ch) request_channels(ch) end |
#start ⇒ Object
43 44 45 46 47 48 |
# File 'lib/oxidized/sshwrapper.rb', line 43 def start raise "MissingSSHLibrary" if !defined? Net::SSH @connection = Net::SSH.start(@ip, @username, password: @password, verbose: @verbose, port: @port, auth_methods: @auth_methods, proxy: @proxy) return yield self if block_given? return (@connection and not @connection.closed?) end |
#start_channel_requests ⇒ Object
121 122 123 |
# File 'lib/oxidized/sshwrapper.rb', line 121 def start_channel_requests create_session end |