处理来自 telnet 命令的响应

Processing response from telnet command

我远程登录到我的测试设备,并在登录后发出一个命令,它给我十行响应。我想将这十行分配给一个数组或任何数据结构以验证响应。例如,我的回复如下所示。我想检查行列表中的点点滴滴。我可能想检查 192brown0903.

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

以下是我的示例代码。如果我将结果分配为数组,则所有数据都分配为第一个元素。

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

根据documentationcmd returns 包含完整输出的字符串。

因此您应该能够使用 split 将其作为每行包含一个字符串的数组获取:

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