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

Hello, I am able to output of static shell commands in CGI for example: ls -l, But what I is I want to execute a command for instanse the output of a wget command in a cgi script and make the cgi script output this data as it comes in. Can I do this, or is there any way or class that I can embedd a shell terminal inside a cgi script?

Replies are listed 'Best First'.
Re: Shell output in perl CGI
by bgreenlee (Friar) on Feb 04, 2005 at 06:45 UTC

    If you're just spitting out the results of a quick shell command, like ls -l or wget on a not-too-slow page, then backticks are a quick-and-dirty solution (you can also do open(CMD,"some/shell/cmd |")). If your intention is to watch a longer process, like 'top', then check out Merlyn's Watching long processes through CGI.

    As an aside, I'd use LWP::Simple or LWP::UserAgent rather than wget.

    -b

      Thanks a lot, your reply solved my question. Thanks once again.
Re: Shell output in perl CGI
by g0n (Priest) on Feb 04, 2005 at 09:44 UTC
    Slightly off topic, but worth mentioning. Be very careful if you are executing a shell command that contains any user supplied input. An attacker could conceivably append a ; and an arbitrary command on the parameter input and have it execute on your web server. Some validation of the input before it is executed would be a good thing

    VGhpcyBtZXNzYWdlIGludGVudGlvbmFsbHkgcG9pbnRsZXNz
      yes; that can be scary, and is along the same lines as SQL Injection.

      Although, some good can come of it. I once had to write a script to bring sshd back up after it crashed from an OpenSSL upgrade messing up.

      Justin
        to clear confusion, since most of you are probably wondering "well -- if sshd was down -- how did he write the script?"
        I wrote the script in notepad; uploaded it via the ftp daemon which was still running; then executed the script by moving it to my public_html dir and going to it from a browser.

        Justin
Re: Shell output in perl CGI
by jpk236 (Monk) on Feb 04, 2005 at 06:05 UTC
    #!/usr/bin/perl $wget = `/usr/local/bin/wget http://www.domain.com`; print $wget;
    w3m is another weapon of choice; each with its perks.

    Justin
      If this actually captured the output of wget it would only diplay after wget had finished running. If my understanding of the backtick operator is correct.

      But maybe if buffering is turned off...?
Re: Shell output in perl CGI
by blueberryCoffee (Scribe) on Feb 04, 2005 at 06:14 UTC
    You can turn off buffering using $| = 1; but I don't think that will help with wget. That is, I don't think wget writes to sdio in a way that perl will capture it - on windows anyway. But you have the html (I forget the tag) reload a cgi script that used ls -l every 5 seconds and get some easy updates.
Re: Shell output in perl CGI
by fraktalisman (Hermit) on Feb 05, 2005 at 13:28 UTC
    Like you said, simple commands work quite well, so it is possible to have a perl interface to access the shell using your browser. To do it really well, certain commands like wget should be treated specially, like the answers stated. Another special command would be man. For the rest, you could use pre-Tags or textareas for the output, and never ever put such a shell perl access online without password protection (.htaccess)!