class Oxidized::Telnet

Constants

RESCUE_FAIL

Attributes

telnet[R]

Public Instance Methods

cmd(cmd_str, expect = @node.prompt) click to toggle source
# File lib/oxidized/input/telnet.rb, line 37
def cmd(cmd_str, expect = @node.prompt)
  Oxidized.logger.debug "Telnet: #{cmd_str} @#{@node.name}"
  return send(cmd_str + "\r\n") unless expect

  # create a string to be passed to oxidized_expect and modified _there_
  # default to a single space so it shouldn't be coerced to nil by any models.
  out = String(' ')
  @telnet.puts(cmd_str)
  @telnet.oxidized_expect(timeout: @timeout, expect: expect, out: out)
  out
end
connect(node) click to toggle source
# File lib/oxidized/input/telnet.rb, line 9
def connect(node)
  @node    = node
  @timeout = Oxidized.config.timeout
  @node.model.cfg['telnet'].each { |cb| instance_exec(&cb) }
  @log = File.open(Oxidized::Config::LOG + "/#{@node.ip}-telnet", 'w') if Oxidized.config.input.debug?
  port = vars(:telnet_port) || 23

  telnet_opts = {
    'Host'    => @node.ip,
    'Port'    => port.to_i,
    'Timeout' => @timeout,
    'Model'   => @node.model,
    'Log'     => @log
  }

  @telnet = Net::Telnet.new telnet_opts
  begin
    login
  rescue Timeout::Error
    raise PromptUndetect, ['unable to detect prompt:', @node.prompt].join(' ')
  end
  connected?
end
connected?() click to toggle source
# File lib/oxidized/input/telnet.rb, line 33
def connected?
  @telnet && (not @telnet.sock.closed?)
end
output() click to toggle source
# File lib/oxidized/input/telnet.rb, line 53
def output
  @telnet.output
end
send(data) click to toggle source
# File lib/oxidized/input/telnet.rb, line 49
def send(data)
  @telnet.write data
end

Private Instance Methods

disconnect() click to toggle source
# File lib/oxidized/input/telnet.rb, line 63
def disconnect
  disconnect_cli
  @telnet.close
rescue Errno::ECONNRESET
  # This exception is intented and therefore not handled here
ensure
  @log.close if Oxidized.config.input.debug?
  (@telnet.close rescue true) unless @telnet.sock.closed?
end
expect(regex) click to toggle source
# File lib/oxidized/input/telnet.rb, line 59
def expect(regex)
  @telnet.oxidized_expect expect: regex, timeout: @timeout
end