ext/pty/expect_sample.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  # sample program of expect.rb
   3  #
   4  #  by A. Ito
   5  #
   6  #  This program reports the latest version of ruby interpreter
   7  #  by connecting to ftp server at netlab.co.jp.
   8  #
   9  require 'pty'
  10  require 'expect'
  11  
  12  fnames = []
  13  PTY.spawn("ftp ftp.netlab.co.jp") do
  14    |r_f,w_f,pid|
  15    w_f.sync = true
  16    
  17    $expect_verbose = true
  18    
  19    r_f.expect(/^Name.*: /) do
  20      w_f.print "ftp\n"
  21    end
  22    
  23    if !ENV['USER'].nil?
  24      username = ENV['USER']
  25    elsif !ENV['LOGNAME'].nil?
  26      username = ENV['LOGNAME']
  27    else
  28      username = 'guest'
  29    end
  30    
  31    r_f.expect('word:') do
  32      w_f.print username+"@\n"
  33    end
  34    r_f.expect("ftp> ") do
  35      w_f.print "cd pub/lang/ruby\n"
  36    end
  37    r_f.expect("ftp> ") do
  38      w_f.print "dir\n"
  39    end
  40    
  41    r_f.expect("ftp> ") do |output|
  42      for x in output[0].split("\n")
  43        if x =~ /(ruby.*\.tar\.gz)/ then
  44           fnames.push $1
  45        end
  46      end
  47    end
  48    begin
  49      w_f.print "quit\n"
  50    rescue
  51    end
  52  end
  53  
  54  print "The latest ruby interpreter is "
  55  print fnames.sort.pop
  56  print "\n"