2014-12-13 7 views
0

간단한 루비 스크립트가 있습니다 - memcacheq 서비스의 대기열 목록을 제공합니다. 텔넷이 연결을 닫지 않습니다.

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 

했지만 다음 스크립트는, 그것은 몇 초 가까이하지 기다리고 않고 오류를 반환했습니다 : 연결이 close 명령 후에 닫혀 있지 왜

/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>' 

?

답변

2

명령이 완료되었음을 알기 위해 "Prompt" regexp를 찾고 있습니다. 워드 프로세서

: stats queue 이후

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 .

사항은 다음과 같이 작동합니다 END로 끝납니다.

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

감사합니다. 그것은 작동합니다. – Eugene

관련 문제