Telnet不关闭连接

问题描述:

拥有简单的ruby脚本 - 从memcacheq服务提供队列列表。Telnet不关闭连接

require 'net/telnet' 

host = Net::Telnet::new("Host" => "127.0.0.1", "Port" => 22201, "Telnetmode" => false) 
host.cmd("stats queue") { |q| puts q } 
host.close 

有一次输出

STAT email_v2_websiteusers 4770/4770 
STAT media_casting 7444/7444 
STAT encoder_v1_job 7479/7479 
STAT pg_generator 163/163 
STAT streaming_session_stats 163756/163756 
STAT pg_export 150/150 
END 

但随后脚本并不密切,它等待几秒钟,返回错误:

/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 memcacheq-metrics.rb:18:in `<main>' 

为什么连接不经过close命令关闭?

它正在寻找"Prompt"正则表达式来知道该命令已完成。

从文档:

Prompt/Match
a regular expression matching the host’s command-line prompt sequence. This is needed by the Telnet class to determine when the output from a command has finished and the host is ready to receive a new command. By default, this regular expression is /[$%#>] z/n .

由于stats queueEND那么这样的事情应该工作结束。

# Change cmd to wait for "END" 
host.cmd("String" => "stats queue", "Match" => /^END/) { |q| puts q } 
host.close 
+0

非常感谢!有用 – Eugene 2014-12-14 10:04:25