Ruby有Expect等效的宝石吗?
我尝试在code.google和rubygems.org上搜索,但遗憾的是它没有显示出来.
仅供参考:Expect是一个Unix自动化和测试工具,由Don Libes编写,作为Tcl脚本语言的扩展,用于交互式应用程序,如telnet,ftp,passwd,fsck,rlogin,tip,ssh等.
我最近花了很多时间来解决这个问题(我坚持1.8.7).我发现了这个问题,这个博文和这个论坛帖子真的很有用.
最后这是我的应用程序代码,如果有人对一个小例子感兴趣(在签名包时将密码传递给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
Run Code Online (Sandbox Code Playgroud)
它几乎没有错误检查,但它只是我需要的.
编辑:更新代码Process.wait(pid)以确保它在继续之前完成,并为1.8.7添加关于此存在的注释.
| 归档时间: |
|
| 查看次数: |
15266 次 |
| 最近记录: |