class Oxidized::Telnet

Constants

RescueFail

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)
  return send(cmd_str + "\r\n") unless expect

  Oxidized.logger.debug "Telnet: #{cmd_str} @#{@node.name}"
  args = { 'String'  => cmd_str,
           'Match'   => expect,
           'Timeout' => @timeout }
  @telnet.cmd args
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 51
def output
  @telnet.output
end
send(data) click to toggle source
# File lib/oxidized/input/telnet.rb, line 47
def send(data)
  @telnet.write data
end

Private Instance Methods

disconnect() click to toggle source
# File lib/oxidized/input/telnet.rb, line 61
def disconnect
  disconnect_cli
  @telnet.close
rescue Errno::ECONNRESET
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 57
def expect(regex)
  @telnet.oxidized_expect expect: regex, timeout: @timeout
end