From: "Ara.T.Howard" Date: 2004-05-18T23:43:53+09:00 Subject: Re: PTY and expect tutorial and examples On 17 May 2004, Bret Jolly wrote: > 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 ===============================================================================