简体   繁体   English

无法从Rails调用/运行/执行ruby脚本

[英]Can not call/run/execute ruby script from Rails

I wanna run a ruby script which will be invoked by a method aftest of a controller. 我想运行一个ruby脚本,该脚本将由控制器的测试方法调用。 when excuting controlle we know the current path is at the root of project. 在执行控制时,我们知道当前路径位于项目的根目录。

FullScanTest.rb : the script i wanna invoke. FullScanTest.rb:我想调用的脚本。 i can run the following command in bash shell , but can not invoked in rails ruby ./public/ruby_lib/af/FullScanTest.rb -i 172.16.178.178 -s 25_10_1 -f auto so i wrote a simple ruby script , which only reply a string . 我可以在bash shell中运行以下命令,但不能在rails ruby​​中调用。/public/ruby_lib/af/FullScanTest.rb-i 172.16.178.178 -s 25_10_1 -f auto所以我写了一个简单的ruby脚本,只响应一个字符串。 it did work~ ruby ./public/ruby_lib/af/test.rb it works both on bash-shell and rails. 它确实起作用了〜ruby ./public/ruby_lib/af/test.rb它在bash-shell和rails上都可以工作。

so what the problem? 那是什么问题呢? permission? 允许? setting ? 设置 ?

Thanks you all in advance~ is there anyway to get the error output when we invoked a script on ROR , i think it doesn't show the error msg. 预先谢谢大家〜当我们在ROR上调用脚本时,无论如何都会得到错误输出,我认为它没有显示错误msg。

i found the ruby script stop at require 'mechanize' and 'nokogiri'.(so i mark them ~ they won't be used in my app) however the script also stop at somewhere not in expectaion , is there any limit to run a script? 我发现ruby脚本停止在require'mechanize'和'nokogiri'。(所以我标记了它们〜它们将不会在我的应用程序中使用)但是该脚本也停止在某个不期望的地方,是否有任何限制可以运行脚本? the script will run for many hours. 该脚本将运行多个小时。 i can execute the command ruby ./public/ruby_lib/af/FullScanTest.rb -i 172.16.178.178 -s 25_10_1 -f auto under prj_root 我可以在prj_root下执行命令ruby ./public/ruby_lib/af/FullScanTest.rb -i 172.16.178.178 -s 25_10_1 -f auto

  require 'open-uri'
  require 'rubygems'

  require 'mechanize'

  require 'logger'
  require 'time'
  require 'nokogiri'
  require 'pp'

** is there anyone to show error , when i call a ruby script by a controller? **当我通过控制器调用ruby脚本时,有没有人显示错误? it's hard for me to debug, it didn't show any information. 我很难调试,它没有显示任何信息。 ** **

def aftest

af_lib="./public/ruby_lib/af/"
prog="FullScanTest.rb"
prog2="test.rb"
params="-i 172.16.178.178 -s 25_10_1 -f auto "
cmd = " ruby #{af_lib}#{prog} #{params}  "
@cmd = %x[ echo #{cmd} ]
res1 = %x[ ruby #{af_lib}#{prog} ]
res2 = %x[ ruby #{af_lib}#{prog2} #{params} --force 2>&1 ]
@res1 = "#{res1}"
@res1 = "#{res2}"

end

i use the view to show the output , but it didn't show any errors 我使用视图显示输出,但未显示任何错误

<h1>Af test lala</h1>

<%= form_tag :action => 'update', :id => @some_object %>
<%= form_tag( { :action => :save, }, { :method => :post }) %>
<h2>Command</h2>
<%= @cmd %>
<h2>Results</h2>
<%= @res1 %>
<h3>H3</h3>
<%= @res2 %>

How are you calling the script. 您如何调用脚本。 Seeing as the the only difference between the script that works and the script that doesn't, is the fact that the script that doesn't work has ARGUMENTS??? 视为有效脚本与无效脚本之间的唯一区别是,无效脚本具有ARGUMENTS的事实???

Do you use system to call the script? 您是否使用系统调用脚本?

ie

system("./public/ruby_lib/af/FullScanTest.rb","-i","172.16.178.178","-s","25_10_1","auto")

or even better 甚至更好

path = "#{Rails.root}/public/ruby_lib/af/FullScanTest.rb"
system(path,"-i","172.16.178.178","-s","25_10_1","auto")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM