简体   繁体   English

如何将表单数据从cgi文件发布到bash脚本?

[英]How to POST form data to a bash script from cgi file?

I've been tasked with creating a simple page and form in Webmin that takes 5 parameters, and sends them to a bash script for further processing. 我的任务是在Webmin中创建一个简单的页面和表单,该表单和表单需要5个参数,并将其发送到bash脚本进行进一步处理。

Nothing fancy, but this is new to me, and I'm unsure how to accomplish this task. 没什么好想的,但这对我来说是新的,我不确定如何完成此任务。

I'm able to manually pass parameters to my bash script like 我能够手动将参数传递给我的bash脚本,例如

sh mySync.sh "1.2.3.4" "user" "password" "abc" "def"

and they they echo out accordingly. 他们会相应地回声。

Here are my files: 这是我的文件:

index.cgi 的index.cgi

#!/usr/bin/perl

require 'mySync-lib.pl';
ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);

$conf = get_mySync_config();
print &text('index_root', $dir),"<p>\n";


print( "<div class='container'>" );
print( "<div class='row'>" );
print( "<h3>MySync</h3>" );
print( "<p>Use this utility to pass params to mySync.sh</p>" );
print( "<form class='form-horizontal' method='POST' action='mySync.sh'>" );

print( "<div class='form-group'>" );
print( "<label for='targetServer' class='col-xs-2 control-label'>Target Server</label>" );
print( "<div class='col-xs-7'>" );
print( "<input type='text' class='form-control' name='targetServer' id='targetServer' placeholder='Target Server'>" );
print( "</div>" );
print( "</div>" );

print( "<div class='form-group'>" );
print( "<label for='userName' class='col-xs-2 control-label'>User Name</label>" );
print( "<div class='col-xs-7'>" );
print( "<input type='text' class='form-control' name='userName' id='userName' placeholder='User Name'>" );
print( "</div>" );
print( "</div>" );

print( "<div class='form-group'>" );
print( "<label for='password' class='col-xs-2 control-label'>Password</label>" );
print( "<div class='col-xs-7'>" );
print( "<input type='password' class='form-control' name='password' id='password'>" );
print( "</div>" );
print( "</div>" );

print( "<div class='form-group'>" );
print( "<label for='srcScope' class='col-xs-2 control-label'>Source Scope</label>" );
print( "<div class='col-xs-7'>" );
print( "<input type='text' class='form-control' name='srcScope' id='srcScope'>" );
print( "</div>" );
print( "</div>" );

print( "<div class='form-group'>" );
print( "<label for='destScope' class='col-xs-2 control-label'>Destination Scope</label>" );
print( "<div class='col-xs-7'>" );
print( "<input type='text' class='form-control' name='destScope' id='destScope'>" );
print( "</div>" );
print( "</div>" );

print( "<div class='form-group'>" );
print( "<div class='col-xs-offset-2 col-xs-10'>" );
print( "<button type='submit' class='btn btn-default'>Send Data to mySync.sh</button>" );
print( "</div>" );
print( "</div>" );

print( "</form>" );
print( "</div>" );
print( "</div> <!- end of container ->" );


ui_print_footer("/", $text{'index'});

mySync.sh mySync.sh

#!/bin/bash

echo "BASH FIELD 1:    $1"
echo "BASH FIELD 2:    $2"
echo "BASH FIELD 3:    $3"
echo "BASH FIELD 4:    $4"
echo "BASH FIELD 5:    $5"

Please let me know if I'm missing a step, or what the next logical step would be. 请让我知道我是否缺少步骤,或者接下来的逻辑步骤是什么。 Thanks! 谢谢!

Writing a CGI program in 2016 seems rather backwards-looking. 在2016年编写CGI程序似乎有些倒退。 I'd recommend installing Plack and writing a PSGI program instead. 我建议安装Plack并编写一个PSGI程序。

But let's assume that you have good reasons for using outdated technology. 但是,假设您有充分的理由使用过时的技术。

You should always start a Perl program with: 您应该始终通过以下方式启动Perl程序:

use strict;
use warnings;

These will show up some problems in your code. 这些将在您的代码中显示一些问题。 For example, I see that you are using a hash called %text ( $text{'index_title'} ) without declaring it anywhere - perhaps that happens in mySync-lib.pl , I have no way of knowing. 例如,我看到您使用的是名为%text$text{'index_title'} )的mySync-lib.pl ,而没有在任何地方声明它-也许发生在mySync-lib.pl ,我无法知道。

If you're writing a CGI program then you should really use CGI.pm to make your life easier. 如果您正在编写CGI程序,则应该使用CGI.pm简化生活。 You can get access to the parameters passed to your program using the param() function. 您可以使用param()函数访问传递给程序的param() If you're using Perl 5.22, thne you'll need to install CGI.pm as it is no longer part of the standard installation. 如果您使用的是Perl 5.22,则需要安装CGI.pm,因为它不再是标准安装的一部分。

You don't need to an individual print() statement for every line of HTML. 您无需为每一行HTML都使用单独的print()语句。 You can print the whole lot on one go with a "heredoc". 您可以使用“ heredoc”一次性打印全部内容。

print <<"END_HTML";
<div class='container'>
... lots more HTML
</div>
END_HTML

But the best approach would be to move your HTML pages out into external files and use a templating system . 但是最好的方法是将HTML页面移到外部文件中并使用模板系统

We haven't needed to use & to call Perl functions for over twenty years. 二十多年来,我们不需要使用&来调用Perl函数。 Please don't do that. 请不要那样做。

Update: Without writing your code for you, the program would probably look something like this: 更新:如果不为您编写代码,该程序可能看起来像这样:

#!/usr/bin/perl

use strict;
use warnings;
use CGI qw[header param];

if (param()) { # parameters have been passed from the form
  # Use param() to get the input parameters
  # Use backticks to run your shell script and collect the output
  # Display output to the users
} else {
  # Display the input form to the users
}

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

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