From: Cliff Rosson Date: 2013-03-20T00:18:32+09:00 Subject: Re: Cisco Ruby pexpect equivalent --20cf3074d626303f9504d8489e49 Content-Type: text/plain; charset=ISO-8859-1 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 wrote: > On Tue, Mar 19, 2013 at 1:14 AM, Steve Jarvis > 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 `
' > irb(main):002:0> require 'expect' > => true > irb(main):003:0> IO.instance_method :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 --20cf3074d626303f9504d8489e49 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
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
=A0 attr_accessor :errors
=A0=A0
=A0 def initialize(creds)
=A0 =A0 begin
=A0 =A0 =A0 @ssh_session =3D Net::SSH.start(creds[:host], = creds[:user], :password =3D> creds[:password], :keys =3D> [])
=A0 =A0 =A0 @ssh =3D Net::SSH::Telnet.new("Session&qu= ot; =3D> @ssh_session, "Prompt" =3D> creds[:prompt])
=A0 =A0 =A0 @errors =3D false
=A0 =A0 rescue Exception =3D> e
=A0 =A0 =A0= @errors =3D e
=A0 =A0 end
=A0 end
=A0=A0
=A0 def cmd(command)
=A0 =A0 @ssh.cmd(command)=
=A0 end
=A0=A0
=A0 def close
=A0 =A0 @ssh.close
=A0 end
=A0 =A0=A0
end



I then wrap it for different devic= es like F5 LTM or Cisco IOS.

creds[:pr= ompt] =3D /.*>|.*#/
ssh.cmd("en\r#{creds[:= enable]}")
ssh.cmd("term len 0")
config =3D @ssh.cmd("sh= ow run")

But if you browse github= there are even more elaborate things people have built out there for devic= e 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 i= t and I'm an automation loon!
=A0


On Tue, Mar 19, 2013 at 12:34 AM, Robert Klemme <sho= rtcutter@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&= quot;?

Does that something like Ruby's IO#expect?

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

$ ri -T IO#expect
IO#expect

(from ruby core)
---------------------------------------------------------------------------= ---
=A0 expect(pat,timeout=3D9999999) { |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. =A0If the timeout expires or eof is found, nil is returned or<= br> yielded. =A0However, the buffer in a timeout session is kept for the next e= xpect
call. =A0The default timeout is 9999999 seconds.


Kind regards

robert

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




--
--20cf3074d626303f9504d8489e49--