2

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.

4
  • Are you sure the output is going to STDOUT? You can test on the shell with openssl ... > /dev/null and if you still see output then it's writing to STDERR instead. Then you'll have to use popen3. Commented Jul 23, 2013 at 15:16
  • I've responded in an edit above. Thanks! Commented Jul 23, 2013 at 16:44
  • 1
    Is that what you put for the popen3 call? It should be in regular quotes, not back-ticks. Commented Jul 23, 2013 at 16:54
  • @mbratch yeah, that was a mistake. tadman pointed me in the right direction, but nevertheless, many thanks for the input Commented Jul 23, 2013 at 17:33

1 Answer 1

1

You're missing a few things. One is that popen3 takes a string argument, and backticks cause an external shell call that coincidentally returns a string.

The backtick convention comes from the bash shell where it's used to inline the result of a command:

ls -l `which ls`

This could expand to:

ls -l '/bin/ls'

With that in mind, what you should have is this:

Open3.popen3('openssl pkcs12 -in bad_certificate.p12 -info -noout -passin pass:""') do |stdin, stdout, stderr, wait_thr|
  # stderr is a standard IO filehandle
  p12_info = stderr.read
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.