简体   繁体   中英

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.

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

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

and they they echo out accordingly.

Here are my files:

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"

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. I'd recommend installing Plack and writing a PSGI program instead.

But let's assume that you have good reasons for using outdated technology.

You should always start a Perl program with:

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.

If you're writing a CGI program then you should really use CGI.pm to make your life easier. You can get access to the parameters passed to your program using the param() function. 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.

You don't need to an individual print() statement for every line of HTML. You can print the whole lot on one go with a "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 .

We haven't needed to use & to call Perl functions for over twenty years. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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