tronmason has asked for the wisdom of the Perl Monks concerning the following question:

Monks. I've created an html page and I'd like to have it run a previously created perl script at the click of a button. While I've got that working, I can't seem to be able to pass the options:

HTML SNIPPET: (This fails only when I pass options i.e. -a -s...)
</form><form action="http://XXX/XXX/XXX/add-hosttest.pl -s server -u u +sername -p password -v view -n name -a ip address" method="post"> <input type="submit" name="sub" value="Run this!"> </form>
PERL SNIPPET: (Shows the GET Options layout)

GetOptions(
"s|server=s" => \$appliance,
"u|username=s" => \$username,
"v|view=s" => \$dnsview,
"n|name=s" => \$fqdn,
"a|address=s" => \$address,
"h|help" => sub { usage("\nTry: $NAME -s IP
-u user -p passwd -v DNSview -n FQDN -a address \n") }, );

When I click on this event, I get a "Page not found" Error message. However, when I run the script without the options and define them in the perl script itself, it works fine...

HTML SNIPPET: (Works just fine)
</form><form action="http://XXX/XXX/XXX/add-hosttest.pl" method="post" +> <input type="submit" name="sub" value="Run this!"> </form>

Perl Snippet: (variables defined) - (Works just fine)

$appliance = 'server';
$username = 'username';
$password = 'password';
$dnsview = 'view';
$fqdn = 'name';
$address = 'X.X.X.X';

Can anyone explain how I can run the Perl script with the options from the html page. Because I'm defining variables there, I'd like to pass them when running the perl script. Any help would be greatly appreciated. Thanks.

Replies are listed 'Best First'.
Re: running a Perl script with options via HTML
by tangent (Parson) on Mar 21, 2014 at 19:45 UTC
    It looks like you are trying to pass command line options via HTTP/CGI which will not work. You can pass parameters using hidden fields in your HTML form:
    <form action="http://XXX/XXX/XXX/add-hosttest.pl" method="post"> <input type="hidden" name="server" value="server_name"> <input type="hidden" name="username" value="my_username"> <input type="hidden" name="view" value="view_name"> <input type="submit" name="sub" value="Run this!"> </form>
    Then, in your Perl script you can access them using the CGI module:
    use CGI; my $q = CGI->new; my $server = $q->param('server'); my $username = $q->param('username'); my $dnsview = $q->param('view');
      Thank you Tangent! You set me straight.
Re: running a Perl script with options via HTML
by afoken (Chancellor) on Mar 21, 2014 at 18:12 UTC
    <form action="http://XXX/XXX/XXX/add-hosttest.pl -s server -u username -p password -v view -n name -a ip address" method="post">

    This looks very wrong. CGIs aren't invoked that way. Parameters passed to the webserver inside the URL (the part after the first ?) are passed to the CGI via environment variables, Parameters passed to the webserver in POST requests are passed to the CGI via STDIN. CGIs do not take command line parameters.

    Read http://en.wikipedia.org/wiki/Common_Gateway_Interface, then consider using CGI or a similar module. Don't try do invent your own CGI protocol handler, you will likely create an incomplete and insecure version.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: running a Perl script with options via HTML
by zentara (Cardinal) on Mar 21, 2014 at 17:21 UTC
    Are you aware of the permissions restrictions on web servers? They usually come in a very low permissions level, and unless you are running suexec ( or similar ), you probably don't have the permissions to run the script. You may have to ssh in to run your script as a priviledged user.

    I'm guessing that you get the symptoms with the options because the script fails differently, and gives you the appearnace that it ran. What does your server logs say?


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      I'm not sure why it would be an issue with permissions because it runs like it should (verified) when I don't pass the parameters but include them in the Perl script as shown. It actually runs and works. The error I get when I pass the parameters is the same as when I do not supply the needed parameters within the perl script.
Re: running a Perl script with options via HTML
by Anonymous Monk on Mar 21, 2014 at 14:22 UTC
      Did you read my post? My question is germane to how to pass parameters via a URL. I already have a webserver that runs my Perl program.