PTY and expect tutorial and examples

B

Bret Jolly

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
 
A

Ara.T.Howard

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

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
===============================================================================
 
B

Bret Jolly

Ara.T.Howard said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top