Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PTY and expect tutorial and examples

610 views
Skip to first unread message

Bret Jolly

unread,
May 17, 2004, 7:05:02 PM5/17/04
to
Is there a tutorial or other simple documentation in English
on how to use PTY and expect? I'm having a heck of a time
trying to figure them out. I also cannot get the example
scripts to work. For example, expect_sample.rb comes with
the ruby distribution (I'm using ruby 1.9.0 (2004-05-10) [i686-linux])
and supposedly uses ftp to ftp.ruby-lang.org to get the name
of the latest stable distribution. Running this script tells
me that the distribution is "nil" and I can't get it to work
no matter how I hack it. Even trying the simplest PTY program
I can think of:

require 'pty'
PTY.spawn("ls") { |r, w, pid| puts r.readlines }

...leads to failure with the exception PTY::ChildExited.

The most infuriating thing is that, despite not knowing what
I am doing, I once had a Ruby expect script which invoked
a function in Pari/gp and took the string result back into
Ruby for further processing. This script no longer works
correctly (it gives answers, but wrong answers). I can't
figure out what is wrong because I don't understand PTY and
expect and I can't find functioning examples or useful
documentation anywhere (the rdoc output is not useful documentation
here). Any pointers to explanations and examples that actually
work will be greatly appreciated. Thanks!

Regards, Bret

Ara.T.Howard

unread,
May 18, 2004, 10:36:12 AM5/18/04
to Bret Jolly

try this (untested on 1.9.0 but the example didn't work with 1.8.1 either -
this does):

#
# sample program of expect.rb
#
# by A. Ito
#
# This program reports the latest version of ruby interpreter
# by connecting to ftp server at netlab.co.jp.
#
require 'pty'
require 'expect'

uri = ARGV.shift || "ftp.ruby-lang.org"
STDOUT.sync = true
STDERR.sync = true
$expect_verbose = true
username = ENV['USER'] || ENV['LOGNAME'] || username = 'guest'
versions = []
login_pat = %r/^\s*name.*:\s*/io
password_pat = %r/^\s*password:/io
prompt_pat = %r/^\s*ftp\s*>\s*/io
version_pat = %r/ruby-(\d\.\d.\d)\.tar\.gz/io

PTY.spawn("ftp ftp.ruby-lang.org") do |r_f,w_f,pid|
w_f.sync = true
r_f.expect(login_pat){ w_f.puts "ftp" }
r_f.expect(password_pat){ w_f.puts "#{ username }@" }
r_f.expect(prompt_pat){ w_f.puts "cd pub/ruby" }
r_f.expect(prompt_pat){ w_f.puts "dir" }
r_f.expect(prompt_pat) do |output|
output.first.each do |line|
m = version_pat.match(line)
versions.push m[1] if m
end
end
w_f.print("bye") rescue nil
end

puts versions.join ','
puts "The latest ruby interpreter is <#{ versions.sort.last }>"

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| "640K ought to be enough for anybody." - Bill Gates, 1981
===============================================================================

Bret Jolly

unread,
May 18, 2004, 2:01:04 PM5/18/04
to
"Ara.T.Howard" <aho...@noaa.gov> wrote in message news:<Pine.LNX.4.44.040518...@fattire.ngdc.noaa.gov>...

> try this (untested on 1.9.0 but the example didn't work with 1.8.1 either -
> this does):
<*snip*>

That didn't work for me either. Looking at it with ruby -rdebug
shows that the variable called output contained just an ftp prompt
rather than the directory listing. Hence versions remained an
empty array and versions.sort.last was nil, resulting in the output
line:


The latest ruby interpreter is <>

Regards, Bret

0 new messages