【问题标题】:How to POST form data to a bash script from cgi file?如何将表单数据从 cgi 文件发布到 bash 脚本?
【发布时间】:2016-05-15 07:16:02
【问题描述】:

我的任务是在 Webmin 中创建一个简单的页面和表单,它接受 5 个参数,并将它们发送到 bash 脚本以进行进一步处理。

没什么特别的,但这对我来说是新的,我不确定如何完成这项任务。

我可以手动将参数传递给我的 bash 脚本,例如

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

他们会相应地回响。

这是我的文件:

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

#!/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"

如果我遗漏了一个步骤,或者下一个合乎逻辑的步骤是什么,请告诉我。 谢谢!

【问题讨论】:

  • 如果你 POST 到一个脚本,那么参数是通过 STDIN 给出的,即从你的 bash 脚本中你必须阅读 STDIN 来获取它们。也许这可能会有所帮助:stackoverflow.com/a/5451943/5830574

标签: bash perl post cgi webmin


【解决方案1】:

在 2016 年编写 CGI 程序似乎相当落后。我建议安装 Plack 并改为编写 PSGI 程序。

但我们假设您有充分的理由使用过时的技术。

您应该始终使用以下命令启动 Perl 程序:

use strict;
use warnings;

这些会在您的代码中显示一些问题。例如,我看到您使用了一个名为 %text ($text{'index_title'}) 的哈希,而没有在任何地方声明它——也许这发生在 mySync-lib.pl 中,我无法知道。

如果您正在编写 CGI 程序,那么您真的应该使用 CGI.pm 来让您的生活更轻松。您可以使用param() 函数访问传递给程序的参数。如果您使用的是 Perl 5.22,则需要安装 CGI.pm,因为它不再是标准安装的一部分。

您不需要为 HTML 的每一行添加单独的 print() 语句。 您可以使用“heredoc”一次性打印全部内容。

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

但最好的方法是将您的 HTML 页面移到外部文件中并使用templating system

二十多年来,我们不需要使用&amp; 来调用 Perl 函数。请不要那样做。

更新:如果不为您编写代码,程序可能看起来像这样:

#!/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
}

【讨论】:

  • 感谢 HTML 提示和最佳实践。我在一个资源有限的环境中,所以我希望你有一个提示,或者为下一步将表单字段数据导入 mySync.sh 文件?谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多