class Oxidized::SSHWrapper

Constants

VERSION

Attributes

connection[R]
debug[R]
exec[R]
ip[R]
login[W]
output[R]
password[R]
password_prompt[W]
port[R]
prompt[R]
pty_options[W]
session[R]
username[R]
username_prompt[W]

Public Class Methods

new(options) click to toggle source
# 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

Public Instance Methods

check_for_connection() click to toggle source
# File lib/oxidized/sshwrapper.rb, line 59
def check_for_connection
  prep_connection unless @session
end
collect_output(params, expectation) click to toggle source
# File lib/oxidized/sshwrapper.rb, line 73
def collect_output(params, expectation)
  send_data((params + "\n"), expectation)
  return @output
end
connected?() click to toggle source
# File lib/oxidized/sshwrapper.rb, line 176
def connected?
  @connection.closed?
end
create_session() click to toggle source
# File lib/oxidized/sshwrapper.rb, line 125
def create_session
  @session = @connection.open_channel do |channel|
    setup_channels(channel)
  end
end
disconnect() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
login() click to toggle source
# 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
prep_connection() click to toggle source
# File lib/oxidized/sshwrapper.rb, line 102
def prep_connection
  return true if @exec
  start_channel_requests
  login
end
prep_proxy(proxy) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/oxidized/sshwrapper.rb, line 160
def reset_output_buffer
  @output = ''
end
sanitize_output_buffer(sub, *regexs) click to toggle source
# 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) click to toggle source
# File lib/oxidized/sshwrapper.rb, line 86
def send(params)
  @session.send_data params
end
send_data(params, expectation) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/oxidized/sshwrapper.rb, line 131
def setup_channels(ch)
  set_data_hook(ch)
  request_channels(ch)
end
start() { |self| ... } click to toggle source
# 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() click to toggle source
# File lib/oxidized/sshwrapper.rb, line 121
def start_channel_requests
  create_session
end