######################################################## #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.
######################################################## #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.
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’
===============================================================================
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
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”