Class: ExecuteTelnet

Inherits:
ExecuteBase show all
Defined in:
lib/teuton/case/execute/execute_telnet.rb

Instance Method Summary collapse

Methods inherited from ExecuteBase

#initialize

Methods included from Verbose

#verbose, #verboseln

Constructor Details

This class inherits a constructor from ExecuteBase

Instance Method Details

#call(input_hostname) ⇒ Object



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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/teuton/case/execute/execute_telnet.rb', line 8

def call(input_hostname)
  action[:conn_type] = :telnet
  hostname = input_hostname.to_s
  ip = config.get((hostname + "_ip").to_sym)
  username = config.get((hostname + "_username").to_sym).to_s
  password = config.get((hostname + "_password").to_sym).to_s
  text = ""
  begin
    if sessions[hostname].nil? || sessions[hostname] == :ok
      h = Net::Telnet.new(
        "Host" => ip,
        "Timeout" => 30,
        "Prompt" => /login|teuton|[$%#>]/
      )
      # "Prompt" => Regexp.new(username[1, 40]))
      # "Prompt" => /[$%#>] \z/n)
      h.(username, password)
      h.cmd(action[:command]) { |i| text << i }
      h.close
      sessions[hostname] = :ok
    else
      text = "TELNET: NO CONNECTION!"
    end
  rescue Net::OpenTimeout
    sessions[hostname] = :nosession
    conn_status[hostname] = :open_timeout
    verbose Rainbow(Application.instance.letter[:error]).red.bright
    log(" ExceptionType=<Net::OpenTimeout> doing <telnet #{ip}>", :error)
    log(" └── Revise host IP!", :warn)
  rescue Net::ReadTimeout
    sessions[hostname] = :nosession
    conn_status[hostname] = :read_timeout
    verbose Rainbow(Application.instance.letter[:error]).red.bright
    log(" ExceptionType=<Net::ReadTimeout> doing <telnet #{ip}>", :error)
  rescue => e
    sessions[hostname] = :nosession
    conn_status[hostname] = :error
    verbose Rainbow(Application.instance.letter[:error]).red.bright
    log(" ExceptionType=<#{e.class}> doing telnet on <#{username}@#{ip}>" \
        " exec: #{action[:command]}", :error)
    log(" └── username=<#{username}>, password=<#{password}>," \
        " ip=<#{ip}>, HOSTID=<#{hostname}>", :warn)
  end
  output = encode_and_split(action[:encoding], text)
  result.exitcode = -1
  result.content = output
  result.content.compact!
end