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

Boris Kuzevanov
  • 1,232
  • 1
  • 12
  • 21
  • Have a look at https://stackoverflow.com/questions/24514307/passing-variable-from-ruby-as-a-password-for-shell – lacostenycoder Nov 21 '18 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"` – Boris Kuzevanov Nov 21 '18 at 22:44

2 Answers2

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
Raj
  • 22,346
  • 14
  • 99
  • 142
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.

richflow
  • 1,902
  • 3
  • 14
  • 21