Created
August 19, 2009 21:32
-
-
Save jmcarbo/170659 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 = false | |
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 }>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment