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

How can I run a system command with perl by pressing a button on browser. I am able to run the following script at command prompt but I am unable to run with a button on browser. Thanks for the help

Replies are listed 'Best First'.
Re: How can I run ?
by aardvark (Pilgrim) on Mar 24, 2001 at 09:49 UTC
    I wrote this a while back to check out the disk space (df) on a Linux box, maybe this will help you out ...

    In my first page (system.cgi);

    <a href="system_process.cgi?action=df">Free Hard Disk Space</a>
    In the second page (system_process.cgi)
    I get the 'action' parameter;
    my $action = $cgi->param('action') || 'default';

    Later in the second page;

    print <<"END"; <h1>System Administration</h1> <table width="450" valign="top" border="0"> <tr> </td> <td width="400" valign="top" align="left"> END if ($action eq 'df' ) { my ($headers_ar, $df_result_ar) = &MySystem::get_df; # print out the table headers print "<table><tr>\n"; foreach my $t_head ( @$headers_ar ) { print "<th>$t_head</th>\n"; } # end table header print "</tr>\n"; foreach my $hold_ar (@$df_result_ar) { print "<tr>"; foreach (@$hold_ar) { print "<td>$_</td>"; } print "</tr>"; } # end table print "</table>\n"; } print <<"END"; </td> </tr> </table> END
    Here is the &MySystem::get_df routine;
    sub get_df { my (@headers,@rows); # put each line the command returns into an array my @df = `df -Ph`; # the first element of the array contains the headers my $temp_df = shift @df; @headers = split (/\s+/, $temp_df); foreach (@df) { my @parsed_row = split (/\s+/, $_); unshift @rows, \@parsed_row; } return (\@headers, \@rows); }
    The hardest part is figuring out exactly what the command is returning and putting it into a data structure. Then use the data structure to display it. I hope this helps, good luck.

    Get Strong Together!!

Re: How can I run ?
by fpi (Monk) on Mar 24, 2001 at 06:58 UTC
    Did you mean to post your script? Because without it, your question is too general.

    But if you need a general answer: just create a button using whatever html or javascript method you want, point the button to your script, have the script run your desired system command, and it should work.

    However, if this is the first time you are trying to get the script to work thru the browser, then there are a lot more places this might be going wrong. Do you have webserver running? Is it configured to execute a perl script? Does the perl script have the appropriate permissions to be executed? What "system command" are you trying to run anyway?

    If none of this makes sense, then tell us how far along you are and be more clear about your situation. If you don't, I fear your question will be reaped....
Re: How can I run ?
by bjelli (Pilgrim) on Mar 24, 2001 at 18:05 UTC

    What you want to do ("I run a system command with perl by pressing a button on browser") is called a CGI script. You need

    1. a program called a webserver on the machine where the command is to be executed. Example: apache
    2. the perl interpreter installed on the machine where the command is to be executed

    when writing the perl script you have to know that:

    1. the script can not take normal input from STDIN, use (the module) CGI instead
    2. the script can write output to stdout, but the first thing you print out has to be "Content-Type: text/plain\n\n" (the line breaks at the end are very important)
    3. there are also some complications with file permissions and users (if we are talking about UNIX here), because the script will be run under the privileges of the webserver, not under your privileges.

    I hope that helps in getting you started.

    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at
Re: How can I run ?
by nysus (Parson) on Mar 24, 2001 at 16:07 UTC
    I'm a spanking newbie so you may want to take this with a grain of salt but...

    isn't it possible to send command line arguments through the URL. My guess is you want to set the form's ACTION to /cgi-bin/yourprogram.cgi?commandlinearguments here. Then the arguments can be got at with the QUERY_STRING variable.

    Please kick me in the ass if I'm giving wrong info here.

Re: How can I run ?
by sierrathedog04 (Hermit) on Mar 24, 2001 at 22:47 UTC
    If your browser clients are running Windows with ActiveState Perl installed then you can run PerlScript.

    Perl script is a client-side scripting language similar to Javascript and VBScript. You then:

    1. write a function in Perl;
    2. surround the Perl function with <script> tags;
    3. include the script in the HTML that the browser downloads; and
    4. create an HTML button and attach the Perl function to the button's onClick event handler.
    Here is a link to ActiveState's PerlScript page.