簡體   English   中英

如何將表單數據從cgi文件發布到bash腳本?

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

我的任務是在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"

請讓我知道我是否缺少步驟,或者接下來的邏輯步驟是什么。 謝謝!

在2016年編寫CGI程序似乎有些倒退。 我建議安裝Plack並編寫一個PSGI程序。

但是,假設您有充分的理由使用過時的技術。

您應該始終通過以下方式啟動Perl程序:

use strict;
use warnings;

這些將在您的代碼中顯示一些問題。 例如,我看到您使用的是名為%text$text{'index_title'} )的mySync-lib.pl ,而沒有在任何地方聲明它-也許發生在mySync-lib.pl ,我無法知道。

如果您正在編寫CGI程序,則應該使用CGI.pm簡化生活。 您可以使用param()函數訪問傳遞給程序的param() 如果您使用的是Perl 5.22,則需要安裝CGI.pm,因為它不再是標准安裝的一部分。

您無需為每一行HTML都使用單獨的print()語句。 您可以使用“ heredoc”一次性打印全部內容。

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

但是最好的方法是將HTML頁面移到外部文件中並使用模板系統

二十多年來,我們不需要使用&來調用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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM