3

I want to run shell command from rails app controller. My app code is:

cmd = "openssl genrsa -des3 -out testfolder/testkey.key 1024"
system cmd 

than shell will ask me password, so if you run this command from terminal you can input password, but if I run from controller I can't.

I don't want to use rails OPENSSL, for some reasons.

I tried to google but I have no result.

Also I tried something like this:

cmd = "openssl genrsa -des3 -out testfolder/testkey.key 1024"
system cmd
system 'echo', '111111Passs'

This is not solution of my problem.

My question is: how to pass password from controller to shell? Than how I can submit this command? (simulate press on ENTER on my keyboard)

Thanks

2
  • Have a look at stackoverflow.com/questions/24514307/… Commented Nov 21, 2018 at 22:13
  • Please can you write example ? it will be something like this ?: cmd = "openssl genrsa -des3 -out testfolder/test.key 1024 -S 111111Password" Commented Nov 21, 2018 at 22:44

2 Answers 2

2
require 'pty'
require 'expect'

PTY.spawn("openssl genrsa -des3 -out testfolder/testkey.key 1024") do |reader, writer|
  reader.expect(/Enter pass phrase/)
  writer.puts("<password>")
end
Sign up to request clarification or add additional context in comments.

4 Comments

please, explain me this line ` reader.expect(/Enter pass phrase for vendor/testkey.key/)` thanks
when the output from the shell matches that string, the password will get inputted into the shell
so I need to copy exactly terminal answer and paste in this line?
I think even regex works. so just try something shorter like Enter pass phrase
1

You can try passing the passphase through an argument (not recommended):

openssl genrsa -des3 -out testfolder/testkey.key -passout pass:SECRET_PASS 1024

A more secure option is to write the passphrase to a temporary file and use this:

openssl genrsa -des3 -out testfolder/testkey.key -passout file:passphrase.txt 1024

Answer shamelessly lifted from this SO thread which has much more detail and better explanations.

2 Comments

please can you explain me what does it mean -passout ?
"-passout arg output file pass phrase source" source for the pass phrase

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.