ruby-on-rails - 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
解决方案
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
推荐阅读
- javascript - 使用 nuxt-link 将事件 Keycode 添加到我的下拉菜单
- python - 如何通过关系将中间模型中的字段序列化为多对多的主模型?
- rxjs - 具有多个可观察对象的 Angular 6 RXJS HttpRequest 进度
- angularjs - AngularJS无法读取未定义的属性“路径”
- angular - 编写通用逻辑以从 API 获取表格排序和分页首选项并相应地显示数据 angular 4
- android - 如何使用 volley 库以 URLencoded 形式发送特殊字符参数而不进行更改
- php - 如何使用 PHP 密码保护网站并保持用户登录?
- javascript - 无限次重复一个函数
- javascript - Knockout k.apply 不是函数
- xml - XPath:选择特定父节点的子节点