Cisco Ruby pexpect equivalent

Hi All,

This is my first post (long time lurker)

Is anyone aware of a compatible library to python's "pexpect"?

What I'm looking for is the pexpect.interact() function in a ruby
library/gem.

I don't see an equivilent in net/ssh, /net/telnet, ruby expect, etc...

This is the excerpt from the python library

"
interact(self, escape_character='\x1d', input_filter=None,
output_filter=None)
This gives control of the child process to the interactive user (the
human at the keyboard). Keystrokes are sent to the child process, and
the stdout and stderr output of the child process is printed."

For a general idea of what I'm using python pexpect.interact for is:

script logs into device
script runs a few commands (show ip interface brief, show log, show ver
etc.)
prints some generalizations (router is receiving errors on port blah
blah, or last user logged in was (user), etc...)
script then turns over control of the router prompt to me the user (this
is the pexpect.interact() part)

Any help would be greatly appreciated.

Thanks,
FamGrudenza

···

--
Posted via http://www.ruby-forum.com/.

Does that something like Ruby's IO#expect?

irb(main):001:0> IO.instance_method :expect
NameError: undefined method `expect' for class `IO'
        from (irb):1:in `instance_method'
        from (irb):1
        from /usr/bin/irb:12:in `<main>'
irb(main):002:0> require 'expect'
=> true
irb(main):003:0> IO.instance_method :expect
=> #<UnboundMethod: IO#expect>

$ ri -T IO#expect
IO#expect

(from ruby core)

···

On Tue, Mar 19, 2013 at 1:14 AM, Steve Jarvis <lists@ruby-forum.com> wrote:

Hi All,

This is my first post (long time lurker)

Is anyone aware of a compatible library to python's "pexpect"?

------------------------------------------------------------------------------
  expect(pat,timeout=9999999) { |result| ... }

------------------------------------------------------------------------------

Reads from the IO until pattern pat matches or the timeout is
over. It returns an array with the read buffer, followed by the matches. If a
block is given, the result is yielded to the block and returns nil.

The optional timeout parameter defines, in seconds, the total time to wait for
the pattern. If the timeout expires or eof is found, nil is returned or
yielded. However, the buffer in a timeout session is kept for the next expect
call. The default timeout is 9999999 seconds.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

I use expect4r for some command line programs to login to cisco devices.
It includes an interact.

https://rubygems.org/gems/expect4r

···

--
Posted via http://www.ruby-forum.com/.

Huh. I use something like this....

This is an extremely old script of mine but still does the job.

require 'rubygems'

require 'net/ssh'

require 'net/ssh/telnet'

class SSH

  attr_accessor :errors

  def initialize(creds)

    begin

      @ssh_session = Net::SSH.start(creds[:host], creds[:user], :password

=> creds[:password], :keys => )

      @ssh = Net::SSH::Telnet.new("Session" => @ssh_session, "Prompt" =>

creds[:prompt])

      @errors = false

    rescue Exception => e

      @errors = e

    end

  end

  def cmd(command)

    @ssh.cmd(command)

  end

  def close

    @ssh.close

  end

end

I then wrap it for different devices like F5 LTM or Cisco IOS.

creds[:prompt] = /.*>|.*#/
ssh.cmd("en\r#{creds[:enable]}")
ssh.cmd("term len 0")
config = @ssh.cmd("show run")

But if you browse github there are even more elaborate things people have
built out there for device automation/interaction. I don't know if you need
pexpect but i am not that familiar with it. As a Sr Neteng I can tell you
I've never used it and I'm an automation loon!

···

On Tue, Mar 19, 2013 at 12:34 AM, Robert Klemme <shortcutter@googlemail.com>wrote:

On Tue, Mar 19, 2013 at 1:14 AM, Steve Jarvis <lists@ruby-forum.com> > wrote:
> Hi All,
>
> This is my first post (long time lurker)
>
> Is anyone aware of a compatible library to python's "pexpect"?

Does that something like Ruby's IO#expect?

irb(main):001:0> IO.instance_method :expect
NameError: undefined method `expect' for class `IO'
        from (irb):1:in `instance_method'
        from (irb):1
        from /usr/bin/irb:12:in `<main>'
irb(main):002:0> require 'expect'
=> true
irb(main):003:0> IO.instance_method :expect
=> #<UnboundMethod: IO#expect>

$ ri -T IO#expect
IO#expect

(from ruby core)

------------------------------------------------------------------------------
  expect(pat,timeout=9999999) { |result| ... }

------------------------------------------------------------------------------

Reads from the IO until pattern pat matches or the timeout is
over. It returns an array with the read buffer, followed by the matches.
If a
block is given, the result is yielded to the block and returns nil.

The optional timeout parameter defines, in seconds, the total time to wait
for
the pattern. If the timeout expires or eof is found, nil is returned or
yielded. However, the buffer in a timeout session is kept for the next
expect
call. The default timeout is 9999999 seconds.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

--
vizualize.me/cliffrosson

Full Disclosure! I'm one of the founders of Scriptrock..try it with our
product.

We are in the middle of readying for launch, but most of the features
you've described we do at the moment and you can log in now :wink:

Cliff Rosson wrote in post #1102299:

Huh. I use something like this....

This is an extremely old script of mine but still does the job.

require 'rubygems'

    @ssh.close

  end

end

I then wrap it for different devices like F5 LTM or Cisco IOS.

creds[:prompt] = /.*>|.*#/
ssh.cmd("en\r#{creds[:enable]}")
ssh.cmd("term len 0")
config = @ssh.cmd("show run")

But if you browse github there are even more elaborate things people
have
built out there for device automation/interaction. I don't know if you
need
pexpect but i am not that familiar with it. As a Sr Neteng I can tell
you
I've never used it and I'm an automation loon!

On Tue, Mar 19, 2013 at 12:34 AM, Robert Klemme

Fu

···

--
Posted via http://www.ruby-forum.com/\.

Reading up on this now and sending to my Neteng team. The opening video
describes our problems to a T but maybe they do for many organizations.
Looks exciting. I look forward to exploring more details.

···

On Thu, Mar 21, 2013 at 3:05 PM, Mike B. <lists@ruby-forum.com> wrote:

Full Disclosure! I'm one of the founders of Scriptrock..try it with our
product.

We are in the middle of readying for launch, but most of the features
you've described we do at the moment and you can log in now :wink:

Cliff Rosson wrote in post #1102299:
> Huh. I use something like this....
>
> This is an extremely old script of mine but still does the job.
>
> require 'rubygems'
>>
>>
>>
>>
>> @ssh.close
>>
>> end
>>
>>
>>
>> end
>>
>>
>>
>
> I then wrap it for different devices like F5 LTM or Cisco IOS.
>
> creds[:prompt] = /.*>|.*#/
> ssh.cmd("en\r#{creds[:enable]}")
> ssh.cmd("term len 0")
> config = @ssh.cmd("show run")
>
> But if you browse github there are even more elaborate things people
> have
> built out there for device automation/interaction. I don't know if you
> need
> pexpect but i am not that familiar with it. As a Sr Neteng I can tell
> you
> I've never used it and I'm an automation loon!
>
>
>
> On Tue, Mar 19, 2013 at 12:34 AM, Robert Klemme

Fu

--
Posted via http://www.ruby-forum.com/\.

--
vizualize.me/cliffrosson