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

All

i have a web cgi form, that take input from a user, and then uses that input to set the server up to run certain commands. so a user would select for example run vmstat: so you grab the input from the form, and then you would like to run vmstat with a time value after it like " vmstat 5 " how would you output this onto the html screen, so the user can see the results.

EG :

my $cmd = $q->param('cmd'); # From html form print <<EndHTML; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 <br>Transitional//EN"> <html> <head> if ($cmd =~ /vmstat/) { printf "Starting to run :$cmd</a>\n"; # Would run something like system("vmstat 5") here } } print <<EndFoot; </body></html> EndFoot

edited by ybiC 2003-07-16 : <code> tags

Replies are listed 'Best First'.
Re: CGI running local shell scripts
by LazerRed (Pilgrim) on Jul 16, 2003 at 16:30 UTC
    While I was stumbling around the monastery the other day, I ran across a link that may be of interest to you.

    Ovid's CGI Course" You should really check it out. Ovid does a real good job of explaining "why" something is bad, rather than just saying "don't do it because I said so".

    LR
Re: CGI running local shell scripts
by Anonymous Monk on Jul 16, 2003 at 16:49 UTC
    Way to completely sidestep the issue, people. minixman, ignore those pedantic wretches who never really attempted to answer your question. To rebuke their points, it is fine to hardcode HTML with here documents in small cases like this if you want to. For larger projects, use the tools they suggested. In answer to your question, the simplest way to do this would be to grab the command param, then wrap it in qx//, assigned to a variable, then print this variable in your here document. A better way would be to have a set of subroutines that handle allowed user commands, parsing out relevant information and adding formatting tags as appropriate to your wants prior to printing.
      Thanks for the information
      i have put the command into a variable like

      $runcomand = `vmstat 5`;
      then tried to print the output, just via print but it does not print it
Re: CGI running local shell scripts
by TomDLux (Vicar) on Jul 16, 2003 at 16:21 UTC

    Don't print hard-coded HTML!

    At the very least, use CGI. Better yet, use a template system. I prefer HTML::Template, but there are others. Ask CPAN about templates.

    As for the output of the commands, invoke the commands wherever you like. Do the commands all have similar output, or does each one need its own format? Obviously, the actual formatting is going to depend on what commands you invoke.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA