Class: Oxidized::SSHWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/oxidized/sshwrapper.rb,
lib/oxidized/ssh/version.rb

Constant Summary collapse

VERSION =
"0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @ip = options[:ip]
  @username = options[:username]
  @password = options[:password]
  @verbose = options[:verbose]
  @debug = options[:debug]
  @prompt = options[:prompt]
  @exec = options[:exec] || false
  @pty_options = options[:pty_options] ||= { term: "vt100" }
  @port = options[:port] ||= 22
  @output = String.new
  @logger = options[:logger] ||= Logger.new(STDOUT)
  @auth_methods = options[:auth_methods] ||= ["publickey", "hostbased", "password"]
  @expectation_handler = options[:expectation_handler]
	@proxy = prep_proxy(options[:proxy])
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/oxidized/sshwrapper.rb', line 10

def connection
  @connection
end

#debugObject (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

#ipObject (readonly)

Returns the value of attribute ip.



10
11
12
# File 'lib/oxidized/sshwrapper.rb', line 10

def ip
  @ip
end

#loginObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/oxidized/sshwrapper.rb', line 108

def 
	if @login
    match = expect @username_prompt, @prompt
	  if match == @login_prompt
 exec @username, @password_prompt
	    exec @password
    end
  else
	  expect @prompt
	end
	
end

#outputObject (readonly)

Returns the value of attribute output.



12
13
14
# File 'lib/oxidized/sshwrapper.rb', line 12

def output
  @output
end

#passwordObject (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

Parameters:

  • value

    the value to set the attribute password_prompt to.



14
15
16
# File 'lib/oxidized/sshwrapper.rb', line 14

def password_prompt=(value)
  @password_prompt = value
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/oxidized/sshwrapper.rb', line 12

def port
  @port
end

#promptObject (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

Parameters:

  • value

    the value to set the attribute pty_options to.



15
16
17
# File 'lib/oxidized/sshwrapper.rb', line 15

def pty_options=(value)
  @pty_options = value
end

#sessionObject (readonly)

Returns the value of attribute session.



12
13
14
# File 'lib/oxidized/sshwrapper.rb', line 12

def session
  @session
end

#usernameObject (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

Parameters:

  • value

    the value to set the attribute username_prompt to.



14
15
16
# File 'lib/oxidized/sshwrapper.rb', line 14

def username_prompt=(value)
  @username_prompt = value
end

Instance Method Details

#check_for_connectionObject



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

Returns:

  • (Boolean)


176
177
178
# File 'lib/oxidized/sshwrapper.rb', line 176

def connected?
  @connection.closed?
end

#create_sessionObject



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

#disconnectObject



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_connectionObject



102
103
104
105
106
# File 'lib/oxidized/sshwrapper.rb', line 102

def prep_connection
  return true if @exec
  start_channel_requests
	
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_bufferObject



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

#startObject



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_requestsObject



121
122
123
# File 'lib/oxidized/sshwrapper.rb', line 121

def start_channel_requests
  create_session
end