Search code examples
rubytelnet

Processing response from telnet command


I telnet into my test device, and after logging in, issue a command which gives me ten lines of response. I want to assign these ten lines to an array or any data structures to validate the response. E.g., my response looks like below. I want to check bits and pieces within the list of lines. I might want to check 192, brown and 0903.

I have a 192 blocks of sand , which are
green brown
yellow is a color of grain
11 0909 0903

Following is my sample code. If I assign the result as array, all data are assigned as first element.

require 'net/telnet'

i = Net::Telnet::new("Host" => '192.111.214.16',
  "Port" => 23,
  "Output_log" => "output.log", # default: nil (no output)
  "Dump_log"   => "dump.log",   # default: nil (no output)
  "Prompt"     => /[#>]/ , # default: /[$%#>] \z/n
  "Telnetmode" => true,         # default: true
  "Timeout"    => 100,           # default: 10
)

i.login("admin", "pass") { |c| print c }
result = i.cmd("String" => "status", "Match" => /a#/)
print result

Solution

  • According to the documentation, cmd returns a string containing the complete output.

    So you should be able to use split to get it as an array containing one string per line:

    result = i.cmd("String" => "status", "Match" => /a#/).split("\n")