从HTML表单运行ruby脚本 [英] Run ruby script from a HTML form

查看:51
本文介绍了从HTML表单运行ruby脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前,我在html/php中创建了一个表单,该表单将用于输入有关新虚拟机的信息.

A couple of months ago I created a form in html/php that would be use to enter informations about new Virtual Machine.

我还一直在研究一个简单的ruby脚本,该脚本可以为新的虚拟机找到新的IP地址.

I've also been working on a simple ruby script that would find a new IP address for a new Virtual Machine.

这两个项目没有关联,但是现在我希望成为一个项目..但是我遇到了困难.我想做的是使用用户在Web表单中提供的信息来运行ruby脚本.

Those two projects were not linked, but now I would like it to be.. but I'm having a hard time. What I'd like to do is to run the ruby script with the informations provided in by the user in the web form.

让我们说用户输入了新的VM名称(vm1)和其他一些东西,例如其VLAN ID(1).我希望能够使用正确的参数(./script.rb --add vm1 --vlanid 1 --...)启动表单.我试图找到一种简单的方法来执行此操作,但没有成功.我安装了旅客,但是我有点迷失了,只安装它来做我想做的事似乎太过分了.

Lets say the user enters the new VM name (vm1) and some other things like its vlan id (1). I'd like to be able to have the form launch the script with the right paramaters (./script.rb --add vm1 --vlanid 1 --...). I tried to find an easy way to do this, but did not manage to. I installed passenger, but I'm kind of lost with it and it seems pretty overkill to install that only to do what I want.

谢谢你们!

推荐答案

您可以使用exec()来做到这一点:

You could do it with exec():

<?php
// TODO: Security checks!
exec("./script.rb --arg1 $arg1 --arg2 $arg2 &");
?>

另一种实现方法是将Web界面添加到Ruby脚本中. Sinatra 是非常简单的Web界面的一个很好的宝石,您可以定义一个基本的处理程序:

Another way to do it is add a web interface to your Ruby script. A great gem for very simple web interfaces is Sinatra, you can just define a basic handler:

require 'rubygems'
require 'sinatra'

get "/" do
  # do what you need to do, all the GET arguments are in the params Hash
end

这篇关于从HTML表单运行ruby脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆