I'm trying to write a little lib which generates a keypair using open4, is this the best way of dealing with system calls with multiple prompts?
require 'rubygems'
require 'open4'
Open4::popen4("sh") do |pid, stdin, stdout, stderr|
stdin.puts "openssl genrsa -des3 -out tmp_priv.pem 2048"
stdin.puts "1234"
stdin.puts "1234"
stdin.close
end
Open4::popen4("sh") do |pid, stdin, stdout, stderr|
stdin.puts "openssl rsa -in tmp_priv.pem -out tmp_public.pem -outform PEM -pubout"
stdin.puts "1234"
stdin.close
end
Open4::popen4("sh") do |pid, stdin, stdout, stderr|
stdin.puts "cat tmp_priv.pem tmp_public.pem >> tmp_keypair.pem"
stdin.close
end
I'm not sure your example is going to do what you want. If run as in your question
opensslis going to open/dev/ttyand it will end up prompting the user despite the pipe. It won't see the1234.If instead you run:
then in that case it will read stdin but it will only need the output file password once. And to answer the question you asked, yes, that's a good way, though it's not a system call.
It's also quite rare on Unix-like systems to need to fake up program input in the first place. You might want to reread the
openssl(1ssl)andgenrsa(1ssl)man pages; they will note various different password source options.