I want to start an FTP process, issue commands, check for a specially formatted response strings and capture them, then issue commands based on the captured strings.
So the flow is:
start ftp
echo the welcome text
for each ftp subcommand
a) issue subcommand
b) echo the response for the subcommand
c) scan the response for a pattern and save the matches for later
perform a computation on the saved patterns
issue more ftp commands
echo the response
Here is my script, which does not read ftp output and hangs after attempting to write the first ftp subcommand:
require 'io/wait'
require 'open3'
# Define the commands to send to FTP
commands = [
'ascii',
'quote site filetype=jes',
'put amblist.jcl',
'dir'
]
user_id = 'mikes01'.downcase
program_command = "ftp ftp://#{user_id}:mypwd@bigiron"
Open3.popen2(program_command) do |stdin, stdout, wait_thread|
Thread.new do
stdout.each do |line|
# I want to scan the line for a pattern but we never get here
puts line
stdout.flush
end
end
commands.each do |command| # Issue command
stdin.puts command
# Hopefully the output resulting from the ftp command is captured in the above thread
end
# Do something with previously processed output before closing the FTP session
stdin.close
wait_thread.value # exit status
end
# Do more processing
ftp
command line tool which provides a REPL seems quite complicated. Wouldn't it be easier to use Ruby'sNet::FTP
library to communicate with the FTP server directly?quote
subcommand was implemented inNet::FTP
, so I wrote it and created a pull request.