Ruby 是否有一个 Expect 等效 gem?

发布于 2024-11-30 20:34:40 字数 261 浏览 0 评论 0 原文

Ruby 是否有一个 Expect 等效 gem?

我尝试在 code.google 和 ruby​​gems.org 上搜索,但遗憾的是它没有出现。

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

Is there an Expect equivalent gem for Ruby?

I tried searching on code.google and rubygems.org, but sadly it did not show up.

FYI: Expect is a Unix automation and testing tool, written by Don Libes as an extension to the Tcl scripting language, for interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

黑色毁心梦 2024-12-07 20:34:40

Ruby 附带了 PTY 模块,用于设置伪终端驱动交互式命令行应用程序。随之而来的是一个 expect 方法 允许您与类似 Expect 的应用程序进行交互。为了学习如何使用expect,我找到了“Ruby Expect 库会带来什么?”有帮助。

就宝石而言,也许可以查看 greenletters ,它应该在 PTY + 的基础上进行改进 + 期望(尽管我还没有)我自己尝试过)。

Ruby comes with the PTY module for setting up pseudoterminals to drive interactive command line applications. With it comes an expect method that allows you to interact with an application kinda like Expect. For learning how to use expect, I found "What to expect from the Ruby expect library?" helpful.

As far as gems go, maybe checkout greenletters which is supposed to improve upon PTY + expect (although I haven't tried it myself).

溺孤伤于心 2024-12-07 20:34:40

我最近花了相当多的时间来解决这个问题(我被 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 

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

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

I recently spent quite a bit of time struggling with this issue (I am stuck with 1.8.7). I found this question, this blog post and this forum thread really useful.

At the end this is my application code if anyone is interested in a little example (pass the password to rpm when signing packages):

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 

It has little error checking but it was all I needed.

Edit: Update the code with Process.wait(pid) to make sure it finishes before continuing and add comment about this being for 1.8.7.

煮酒 2024-12-07 20:34:40

查看这个 ruby​​gem:https://github.com/abates/ruby_expect。它可以为你处理一些小任务。从其官方示例来看,“输入密码”并登录并与本地脚本交互就足够了。

这是一个更新 git 代码(使用密码进行身份验证)的示例:

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 

[email protected]??.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.

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

checkout this rubygem: https://github.com/abates/ruby_expect. It could handle some small task for you. from its official example, it's enough to 'enter password' and login and interactive with local script.

here is an example that update the git code (which is authenticated with password):

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

just run the code above, and your will see like this:

$ ruby update_code.rb 

[email protected]??.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.

for more example and details, please dive into its source code.

望她远 2024-12-07 20:34:40

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

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

expect4r seems to do what you are asking for, though it is made specific for connections to Cisco and Juniper devices.

Perhaps even better is yax as this is "yet another expect".

罗罗贝儿 2024-12-07 20:34:40

RExpect

来自项目的网站:

RExpect 是对expect.rb 模块的替代品。
标准库更快、更健壮,可驱动
许多设备同时使用。

RExpect

From the project's website:

RExpect is a drop in replacement for the expect.rb module in the
standard library that is faster and more robust, cabable of driving
many devices simultaneously.

才能让你更想念 2024-12-07 20:34:40

parley 是另一种你可以尝试的方法(由我编写)。它的灵感来自 Perl Expect。

parley is another one you can try, (written by me). It is inspired by Perl expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文