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.
| [reply] [d/l] |
|
|
Thanks a lot, your reply solved my question.
Thanks once again.
| [reply] |
Re: Shell output in perl CGI
by g0n (Priest) on Feb 04, 2005 at 09:44 UTC
|
| [reply] |
|
|
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
| [reply] |
|
|
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
| [reply] |
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 | [reply] [d/l] |
|
|
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...?
| [reply] |
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. | [reply] |
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)!
| [reply] |