Ruby有Expect等效的gem吗?

Ruby有Expect等效的gem吗?

我尝试在code.google和rubygems.org上搜索,但遗憾的是它没有显示出来。

仅供参考: Expect是一个Unix自动化和测试工具,由Don Libes编写,作为Tcl脚本语言的扩展,用于交互式应用程序,如telnet,f​​tp,passwd,fsck,rlogin,tip,ssh等。

Ruby附带PTY模块,用于设置伪终端以驱动交互式命令行应用程序。 随之而来的是一种expect方法 ,它允许您与Expect等应用程序进行交互。 为了学习如何使用expect,我发现“ 对Ruby期望库有什么期望? ”很有帮助。

对于gem来说,也许结帐绿色小册子应该会对PTY +期望有所改善(尽管我自己没有尝试过)。

我最近花了很多时间来解决这个问题(我坚持1.8.7)。 我发现这个问题 ,这篇博文和这个论坛post确实很有用。

最后这是我的应用程序代码,如果有人对一个小例子感兴趣(在签名包时将密码传递给rpm):

 def run_interactive command, password, promt output = '' begin r, w, pid = PTY.spawn(command) puts r.expect(promt) sleep(0.5) w.puts(password) begin r.each { |l| output += l } rescue Errno::EIO end $?.exitstatus Process.wait(pid) rescue PTY::ChildExited => e $stderr.puts "The child process #{e} exited! #{$!.status.exitstatus}" end output end password = "mypassword" command = "rpm --define '_signature gpg' --define '_gpg_name #{key_id}' --addsign #{package}" promt = %r{.*: } expected = %r{good} output = run_interactive(command, password, promt) if output.match(expected) puts output else abort "Error: expected: '#{expected}' got '#{output}'" end 

它几乎没有错误检查,但它只是我需要的。

编辑:使用Process.wait(pid)更新代码,以确保它在继续之前完成,并为1.8.7添加有关此内容的注释。

结帐这个rubygem: https : //github.com/abates/ruby_expect 。 它可以为你处理一些小任务。 从其官方示例中,它足以“输入密码”并登录并与本地脚本交互。

这是一个更新git代码(使用密码validation)的示例:

 require 'rubygems' require 'ruby_expect' def update_code password = 'your password here' exp = RubyExpect::Expect.spawn('git pull', :debug => true) exp.procedure do each do expect /password: / do send password end end end end update_code 

只需运行上面的代码,你会看到这样的:

 $ ruby update_code.rb shensiwei@gforge.1ver??.net's password: remote: Counting objects: 133, done. remote: Compressing objects: 100% (84/84), done. remote: Total 85 (delta 62), reused 0 (delta 0) Unpacking objects: 100% (85/85), done. 

有关更多示例和详细信息,请深入了解其源代码。

expect4r似乎可以满足您的要求,尽管它专门用于连接Cisco和Juniper设备。

也许更好的是yax,因为这是“另一种期望”。

RExpect

从项目的网站:

RExpect是标准库中expect.rb模块的替代品,它更快,更强大,可同时驱动多个设备。

帕利是你可以尝试的另一个,(由我写)。 它的灵感来自Perl的期待。