Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > expect.rb

Reply
Thread Tools

expect.rb

 
 
Theodore Knab
Guest
Posts: n/a
 
      02-11-2004
Does anybody know where I can get the expect.rb ?

As defined, here in Japanese:
http://www.ruby-lang.org/ja/man/inde...name=expect.rb

See the ab-lib translation below:

################################################## ######
#ab-lib Translated Japanese
################################################## ######

The Expect Library is tlc's Expect package. In Ruby it is part of the IO class.

Example usage:
IO#expect(pattern, timeout=99999)

With the expect method you can directly interact with inline text
interfaces. Expect uses string patterns and Regexp to read the input
from device.

One example, of a piece of equipment where Expect could be
used might be a router with either ssh or telnet enabled. Expect can be
called in a Ruby program to process the inline text on the host device.

When an expected pattern is found, a response will be sent. If the
pattern does not appear, the timeout will expire and move to the next
statement. Practically, Expect has been used to reset passwords on routers.
Expect has been used to run nightly updates on telephone switches.
Expect has been used to setup HP printers. Additionally, expect can be
used to modify any device with a inline text interface.

--
------------------------------------------
Ted Knab
Chester, MD 21619
------------------------------------------
35570707f6274702478656021626f6c6964796f6e602f66602 478656
02e6164796f6e60237471647560216e6460276c6f62616c602 57e696
4797e2a0


 
Reply With Quote
 
 
 
 
Theodore Knab
Guest
Posts: n/a
 
      02-11-2004
I found some example expect code for Ruby now I just need to find the
library:

http://blade.nagaokaut.ac.jp/cgi-bin...uby-list/30822

http://rubystuff.org/treasures/RubyT...test_expect.rb

http://www.ksky.ne.jp/~sakae/d2/20518.html

http://blade.nagaokaut.ac.jp/cgi-bin...uby-list/35746

On 11/02/04 13:37 +0900, Theodore Knab wrote:
> Does anybody know where I can get the expect.rb ?
>
> As defined, here in Japanese:
> http://www.ruby-lang.org/ja/man/inde...name=expect.rb
>
> See the ab-lib translation below:
>
> ################################################## ######
> #ab-lib Translated Japanese
> ################################################## ######
>
> The Expect Library is tlc's Expect package. In Ruby it is part of the IO class.
>
> Example usage:
> IO#expect(pattern, timeout=99999)
>
> With the expect method you can directly interact with inline text
> interfaces. Expect uses string patterns and Regexp to read the input
> from device.
>
> One example, of a piece of equipment where Expect could be
> used might be a router with either ssh or telnet enabled. Expect can be
> called in a Ruby program to process the inline text on the host device.
>
> When an expected pattern is found, a response will be sent. If the
> pattern does not appear, the timeout will expire and move to the next
> statement. Practically, Expect has been used to reset passwords on routers.
> Expect has been used to run nightly updates on telephone switches.
> Expect has been used to setup HP printers. Additionally, expect can be
> used to modify any device with a inline text interface.
>
> --
> ------------------------------------------
> Ted Knab
> Chester, MD 21619
> ------------------------------------------
> 35570707f6274702478656021626f6c6964796f6e602f66602 478656
> 02e6164796f6e60237471647560216e6460276c6f62616c602 57e696
> 4797e2a0
>


--
------------------------------------------
Ted Knab
Chester, MD 21619
------------------------------------------
35570707f6274702478656021626f6c6964796f6e602f66602 478656
02e6164796f6e60237471647560216e6460276c6f62616c602 57e696
4797e2a0


 
Reply With Quote
 
 
 
 
Theodore Knab
Guest
Posts: n/a
 
      02-11-2004
If there is a better module let me know.

Never mind I found a working expect module which I added to '/usr/lib/ruby/1.8/expect.rb':

$expect_verbose = false

class IO
def expect(pat,timeout=9999999)
buf = ''
case pat
when String
e_pat = Regexp.new(Regexp.quote(pat))
when Regexp
e_pat = pat
end
while true
if IO.select([self],nil,nil,timeout).nil? then
result = nil
break
end
c = getc.chr
buf << c
if $expect_verbose
STDOUT.print c
STDOUT.flush
end
if mat=e_pat.match(buf) then
result = [buf,*mat.to_a[1..-1]]
break
end
end
if block_given? then
yield result
else
return result
end
nil
end
end

Additionally, this expect test worked pretty good for me:
http://www.ksky.ne.jp/~sakae/d2/20518.html

--- sample expect ---------------------------------
require 'pty'
require 'expect'

x = ''
PTY.spawn("telnet foo.bar.com") do # Do connect
|r_f,w_f,pid|
w_f.sync = true
$expect_verbose = false ### if true, Can monitor session

status = r_f.expect('login: ',timeout=30) ### login
if status.nil?
puts "No responce foo.bar.com"
exit
end
w_f.print "hoge\n"
r_f.expect('word: ') do
w_f.print "hoge-password\n"
end

r_f.expect('foo% ') do ### DO ls
w_f.print "ls -l\n"
end
r_f.expect('foo% ') do |output|
x = output[0]
end

begin ### logout
w_f.print "logout\n"
rescue
end
end

print "\n=====\nhost foo hoge's home-dir files are \n"
print x
print "\n"
-------------------------------------------------------------------



--
------------------------------------------
Ted Knab
Chester, MD 21619
------------------------------------------
35570707f6274702478656021626f6c6964796f6e602f66602 478656
02e6164796f6e60237471647560216e6460276c6f62616c602 57e696
4797e2a0


 
Reply With Quote
 
Ara.T.Howard
Guest
Posts: n/a
 
      02-11-2004
On Wed, 11 Feb 2004, Theodore Knab wrote:

> Date: Wed, 11 Feb 2004 13:37:18 +0900
> From: Theodore Knab <(E-Mail Removed)>
> Newsgroups: comp.lang.ruby
> Subject: expect.rb
>
> Does anybody know where I can get the expect.rb ?

<snip>

what kind of process are you trying to 'expect' ?

-a
--

ATTN: please update your address books with address below!

================================================== =============================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
================================================== =============================

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments