i am trying to fork a process to run a simple script which requires a
username and password. i made a test case with a simple bash script
and a simple ruby script; however, the ruby script hangs in
stdout.read???
i am new to ruby, so i am assuming i am doing something wrong can
anyone help?
p stdin, stdout, stderr
done = 0
until done == 1
begin
# using IO#readpartial instead of IO#read
line = stdout.readpartial( 4096 )
puts "#{line}"
puts line.eql?("username: ")
if line.eql?("username: ")
puts "myuser"
stdin.write "myuser\n"
# using IO#readpartial instead of IO#read
line = stdout.readpartial( 4096 )
if line.eql?("password: ")
puts "mypassword"
stdin.write "mypassword\n"
done = 1
end
puts "im here!"
end
rescue => err
p err
end
end
end
thanx that did work; however, i after doing more research i agree this
is not the best way. i am a c/c++, java/groovy guy...this ruby stuff
is all new to me; however, after day one; i like it:-)
···
On Apr 8, 4:03 am, "Y. NOBUOKA" <nobu...@r-definition.com> wrote:
Hi,
Using IO#readpartial [1] instead of IO#read, you'll see the ruby
script run as expected.
However, I don't know if this is the best way or not.
p stdin, stdout, stderr
done = 0
until done == 1
begin
# using IO#readpartial instead of IO#read
line = stdout.readpartial( 4096 )
puts "#{line}"
puts line.eql?("username: ")
if line.eql?("username: ")
puts "myuser"
stdin.write "myuser\n"
# using IO#readpartial instead of IO#read
line = stdout.readpartial( 4096 )
if line.eql?("password: ")
puts "mypassword"
stdin.write "mypassword\n"
done = 1
end
puts "im here!"
end
rescue => err
p err
end
end
end
----------------------------------------------------------------------
ruby script
----------------------------------------------------------------------
require 'pty' # found in ruby source @ /ext/pty
require 'expect' # found in ruby source @ /ext/pty/lib/expect.rb
#buffer stores the output from the child process
buffer = ""
# spawn a child process (fork)
PTY.spawn("./myScript.bash") do |output, input, pid|
input.sync = true
#set the expect verbosity flag to false or you will get output from
expect
$expect_verbose = false
#get user from environment if posible
if !ENV['USER'].nil?
username = ENV['USER']
else
username = 'guest'
end
#expect the username prompt and return the username
output.expect('username: ') do
input.puts(username)
end
#expect the password prompt and return the password
output.expect('password: ') do
input.puts 'thePassword'
end
#throw away the password comming back to thru the output (note: you
still get the '\n')
output.expect('thePassword') do
end
#read all the output from the process and put it in a buffer for
later #keep reading until the EOFError exception is thrown
done = 0
while done == 0
begin
buffer += output.readpartial(1024)
rescue EOFError
done = 1
end
end
end
puts "myExcept.rb script ran myScript.bash the results are: \n
#{buffer}"