I am having trouble saving the output of a command in console into Ruby as a variable. I am trying to save information of a .p12 file in as a variable p12_info. This is what I have tried so far.
file = File.read("certificate.p12")
p12 = OpenSSL::PKCS12.new(file, "")
p12_info = `openssl pkcs12 -in #{p} -info -noout -passin pass:""`
print "Info: "
puts p12_info
And this is the output I'm getting:
File name: certificate.p12
MAC Iteration 1
MAC verified OK
PKCS7 Encrypted data: pbeWithSHA1And40BitRC2-CBC, Iteration 2048
Certificate bag
Certificate bag
PKCS7 Data
Shrouded Keybag: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 2048
Shrouded Keybag: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 2048
Info:
The console command seems to be running when I try to set the variable p_12, but doesn't actually get saved into p12_info.
Alternatively if I try this:
p12_info = `echo "foo`
print "Info: "
puts p12_info
Then I get this output, which is what Im going for:
File name: certificate.p12
Info: foo
Any ideas about why this could be happening will be greatly appreciated.
EDIT:
@tadman - Thanks so much for the help. You are correct, the command did in fact output with the appended > /dev/null. Unfortunately, I am having trouble figuring out how to use popen3. I am very unfamiliar with all this..I've tried:
Open3.popen3(`openssl pkcs12 -in bad_certificate.p12 -info -noout -passin pass:""`) {|stdin, stdout, stderr, wait_thr|
pid = wait_thr.pid # pid of the started process.
p12_info = wait_thr.stderr # Process::Status object returned.
}
To no avail. Any pointers that could lead me in the right direction? Much appreciated.
openssl ... > /dev/nulland if you still see output then it's writing to STDERR instead. Then you'll have to usepopen3.popen3call? It should be in regular quotes, not back-ticks.