通过Ruby popen与交互式流程进行通信

时间:2014-04-13 20:38:31

标签: mysql ruby telnet popen

我一直在搞乱IO#popen和不同的程序,并且在交互过程中遇到了一些麻烦。

这是脚本的精简版本:

def test(command, string)
  IO.popen(command, 'a+') do |pipe|
    puts "Prompt: #{pipe.read(5)}"  # Just to show whether data is read in
    pipe.puts string
  end
end

我看到了几种不同的交互过程的各种行为,并试图了解原因。

$ test('pt-kill --user user --ask-pass --print', 'password')
=> This successfully reads in the prompt, and the password is successfully written 
   to the script. Works as desired. (This is a perl script from Percona)

$ test('telnet', 'quit')
=> Blocks indefinitely trying to read the prompt. In the process of hacking around,
   found that calling 'pipe.close_write' prior to the read would allow the read to
   complete. Why?

$ test('mysql -u user -p -e "SELECT 1 FROM DUAL", 'password')
=> Echoes full prompt to the screen, but is still blocking on the first read. 
   Adding a 'pipe.close_write' does nothing.

我一直试图了解这些差异,但我不知所措。有人有解释吗?

0 个答案:

没有答案