Class: TrainPlugins::Telnet::Connection

Inherits:
Train::Plugins::Transport::BaseConnection
  • Object
show all
Defined in:
lib/train-telnet/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



8
9
10
# File 'lib/train-telnet/connection.rb', line 8

def initialize(options)
  super(options)
end

Instance Method Details

#closeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/train-telnet/connection.rb', line 12

def close
  return if @session.nil?

  unless @options[:teardown]&.empty?
    logger.debug format("[Telnet] Sending teardown commands to %s:%d", @options[:host], @options[:port])
    execute_on_channel(@options[:teardown])
  end

  logger.info format("[Telnet] Closed connection to %s:%d", @options[:host], @options[:port])
  session.close
ensure
  @session = nil
end

#create_sessionObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/train-telnet/connection.rb', line 77

def create_session
  logger.info format("[Telnet] Opening connection to %s:%d", @options[:host], @options[:port])

  @session = Net::Telnet.new(
    "Host"   => @options[:host],
    "Port"   => @options[:port],
    "Prompt" => Regexp.new(@options[:prompt_pattern])
  )

  @session.(
    "Name"           => @options[:user],
    "Password"       => @options[:password],
    "LoginPrompt"    => Regexp.new(@options[:login_prompt]),
    "PasswordPrompt" => Regexp.new(@options[:password_prompt])
  )

  unless @options[:setup].empty?
    logger.debug format("[Telnet] Sending setup commands to %s:%d", @options[:host], @options[:port])

    execute_on_channel(@options[:setup])
  end

  @session
end

#execute_on_channel(cmd, &data_handler) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/train-telnet/connection.rb', line 37

def execute_on_channel(cmd, &data_handler)
  if @options[:debug_telnet]
    logger.debug "[Telnet] => #{cmd}\n"
  end

  stdout = session.cmd(cmd)
  stderr = ""
  exit_status = 0

  # Output needs to be flushed first, so we don't skip a beat (Check why)
  session.waitfor("Waittime" => 0.1, "Match" => /./, "Timeout" => 0.1) rescue Net::ReadTimeout

  # Remove \r in linebreaks
  stdout.delete!("\r")

  if @options[:debug_telnet]
    logger.debug "[Telnet] <= '#{stdout}'"
  end

  # Extract command output only (no leading/trailing prompts)
  unless @options[:raw_output]
    stdout = stdout.match(/#{Regexp.quote(cmd.strip)}\n(.*?)\n#{@options[:prompt_pattern]}/m)&.captures&.first
  end
  stdout = "" if stdout.nil?

  # Simulate exit code and stderr
  errors = stdout.match(/^(#{@options[:error_pattern]})/)
  if errors
    exit_status = 1
    stderr = errors.captures.first
    stdout.gsub!(/^#{@options[:error_pattern]}/, "")
  end

  [exit_status, stdout, stderr]
end

#reset_sessionObject



102
103
104
# File 'lib/train-telnet/connection.rb', line 102

def reset_session
  @session.close
end

#run_command_via_connection(cmd, &data_handler) ⇒ Object



30
31
32
33
34
35
# File 'lib/train-telnet/connection.rb', line 30

def run_command_via_connection(cmd, &data_handler)
  logger.debug format("[Telnet] Sending command to %s:%d", @options[:host], @options[:port])
  exit_status, stdout, stderr = execute_on_channel(cmd, &data_handler)

  CommandResult.new(stdout, stderr, exit_status)
end

#session(retry_options = {}) ⇒ Object



73
74
75
# File 'lib/train-telnet/connection.rb', line 73

def session(retry_options = {})
  @session ||= create_session
end

#uriObject



26
27
28
# File 'lib/train-telnet/connection.rb', line 26

def uri
  "telnet://#{options[:user]}@#{@options[:host]}:#{@options[:port]}/"
end