Ruby, Telnet, read multiline response without timeout

I need hints / help, how can I read a multi-line response in a variable. My current command gives me a multi-line response, but after that I get a timeout.

Here's how my connection is made:

connection = Net::Telnet.new('Host' => host,'Port' => 4800, 'Telnetmode' => false, 'Timeout' => 1)

Here is my request and how I save it:

puts "Weather request\n"
connection.cmd("{weather}"){ |c| print c }
parsed = JSON.parse(str)
puts "#{parsed}\n\n"

And here is the error:

/usr/lib/ruby/1.9.1/net/telnet.rb:558:in `waitfor': timed out while waiting for more data (Timeout::Error)
    from /usr/lib/ruby/1.9.1/net/telnet.rb:695:in `cmd'
    from ruby_check.rb:37:in `<main>'

My answer is a few JSON lines, for example:

{"City":"Tallinn", "Degrees":"23"}
{"City":"Berlin", "Degrees":"23"}
{"City":"Helsinki", "Degrees":"23"}
{"City":"Stockholm", "Degrees":"23"}
+4
source share
2 answers

Why time out?

Net::Telnetthe documentation says:

Prompt , Telnet cmd(); , cmd(), puts() waitfor(); sysread() waitfor() .

Net::Telnet#cmd , , :

, .

Prompt Match, #cmd - , Net::Telnet (/[$%#>] \z/n), , , .

- , , , , , , Net::Telnet. , command:, :

connection = Net::Telnet.new(
  "Prompt" => /command: \z/,
  # …
)

, , , , Match, #cmd. , JSON, ], :

connection.cmd("String" => "{weather}", "Match" => "]") { |c| print c }

Net::Telnet TCPSocket

, Net::Telnet, TCPSocket, #cmd:

connection.puts("{weather}")
connection.sock.readline

Net::Telnet TCPSocket.

+2

- , str. - false. Believieng .cmd, :

connection = Net::Telnet.new(
  "Host" => host, "Port" => 4800, 
  "Telnetmode" => false, "Timeout" => false)

puts "Weather request...\n"

str = connection.cmd("{weather}"){ |c| print c }
parsed = JSON.parse(str)

puts "#{parsed}\n\n"
+1

Source: https://habr.com/ru/post/1629596/


All Articles