首页 > 解决方案 > rails run shell command with user input

问题描述

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

标签: ruby-on-railsrubylinuxshell

解决方案


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

推荐阅读