Is there an Expect equivalent gem for Ruby?

前端 未结 6 705
一生所求
一生所求 2020-12-05 14:16

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

6条回答
  •  囚心锁ツ
    2020-12-05 14:50

    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 
    
    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.
    

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

提交回复
热议问题