http://www.perlmonks.org?node_id=207881

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

I have a simple script with one form field that I can type in a unix command (del test.html). It sends the command and refreshes. But I just never know if the command worked or what, It runs from a browser (obviously), and I was wondering if there was a way (actually in know there is but how is the real question) to have it show the reply to the command.

If I were typing in the enviroment and I didn't have permissions it would give me an error, when using my browser script how can I have it, when it refreshes, to show whatever error I would get?

I think ya get what I'm asking, any help would be great, thanks!

Replies are listed 'Best First'.
Re: Show Unix Talk?
by sauoq (Abbot) on Oct 25, 2002 at 00:51 UTC
    I think ya get what I'm asking, any help would be great, thanks!

    Yes, I think I get what you are asking but rather than just answering I have to suggest that you rethink why you are asking.

    There is almost never a good reason to allow arbitrary commands to be run via a CGI application. At best it is error prone and at worst it is a serious security hole. It is very unlikely that you really need to turn your browser into a shell prompt.

    But if you insist on shooting yourself in the foot, use backticks.

    Update: Given that the title of your node suggests that you might want to write a CGI interface to the unix talk(1) command, I thought I should point out that it won't be easy. You'd be better off writing your own talk client and even then it would be pretty horrid. By design, the web is stateless. Talk, on the other hand, is not. You'd have to keep a process running server for each talk session. One way or the other, you would end up with a pretty nasty design.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Show Unix Talk?
by hacker (Priest) on Oct 25, 2002 at 11:58 UTC
    Or, you could do it in a more proper fashion by using IPC::Open3 and capture the success or failure of that command you're executing in the shell. `backticks` are something you should never EVER use, ever. If you insist on using them, ALWAYS make sure to use the /full/path/to/binary in the `call` you use.

    You could also use "system()-in-list-mode" to try to get what you want, and make sure to wrap your calls in something that can return success or failure.

    Alternately, you can do something like:

    # sets $list to 0 if the ls worked $list = system("ls") or die "Failed; $!";

    You may have to set up your own signal handler to capture SIGPIPE and others. If you use `backticks` to execute this command, $? will not be set to what you expect (check perlvar for more details on what $? means).

    perlfaq8 also has a section on this exact topic. Give it a read, it may prove enlightening.

    While I agree with sauoq about NOT turning your web browser into a remote shell, his suggestion of using `backticks` for this is slightly incorrect, given your request (though I see his sarcasm in suggesting it). Backticks will execute a system command and return what was sent to STDOUT by that command. You only want to test the success or failure of that system command, two very different things. Good luck!