Class: Oxidized::Telnet

Inherits:
Input
  • Object
show all
Defined in:
lib/oxidized/input/telnet.rb

Constant Summary

Constants inherited from Input

Input::RESCUE_FAIL

Instance Attribute Summary collapse

Attributes included from Input::CLI

#node

Instance Method Summary collapse

Methods inherited from Input

config_name, #config_name, rescue_fail, to_sym, #to_sym

Methods included from Input::CLI

#connect_cli, #disconnect_cli, #get, #initialize, #login, #newline, #password, #post_login, #pre_logout, #username

Methods included from Config::Vars

#vars

Instance Attribute Details

#telnetObject (readonly)

Returns the value of attribute telnet.



6
7
8
# File 'lib/oxidized/input/telnet.rb', line 6

def telnet
  @telnet
end

Instance Method Details

#cmd(cmd_str, expect = @node.prompt) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oxidized/input/telnet.rb', line 38

def cmd(cmd_str, expect = @node.prompt)
  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(' ')
  @text_debug&.send_data(cmd_str)
  @telnet.puts(cmd_str)
  @telnet.oxidized_expect(timeout: @timeout, expect: expect, out: out)
  out
end

#connect(node) ⇒ Object

rubocop:disable Naming/PredicateMethod



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/oxidized/input/telnet.rb', line 8

def connect(node) # rubocop:disable Naming/PredicateMethod
  @node    = node
  @timeout = @node.timeout
  @node.model.cfg['telnet'].each { |cb| instance_exec(&cb) }

  @text_debug = DebugText.new(Oxidized.config.input.debug, @node, config_name)

  port = vars(:telnet_port) || 23

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

  @telnet = Net::Telnet.new telnet_opts
  begin
    
  rescue Timeout::Error
    raise PromptUndetect, ['unable to detect prompt:', @node.prompt].join(' ')
  end
  connected?
end

#connected?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/oxidized/input/telnet.rb', line 34

def connected?
  @telnet && (not @telnet.sock.closed?)
end

#outputObject



56
57
58
# File 'lib/oxidized/input/telnet.rb', line 56

def output
  @telnet.output
end

#send(data) ⇒ Object



51
52
53
54
# File 'lib/oxidized/input/telnet.rb', line 51

def send(data)
  @text_debug&.send_data(data)
  @telnet.write data
end