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

Greetings monks. I have this weird thing I need to do.
1) I have a perl script doing SSH to other box and executing some commands remotely. It works just fine, no problems here.

2) I need to execute that script from PHP web page, so I wrote this little script

<? echo "HELLO WORLD!"; exec("/usr/bin/sudo perl /usr/local/door/web/ftp.pl", $out, $err); /* if($err){ echo $err; print_r( $out ); } */ ?> ~
Unfortunately I can not get this to work.

Any word of advice on this problem.

Thank you in advance!!!

2005-09-29 Edit by castaway - retitled
Original title: 'PHP && PERL'

Replies are listed 'Best First'.
Re: Execute a perl program from a php web page
by Happy-the-monk (Canon) on Sep 27, 2005 at 21:32 UTC

    When you execute any programme through a webserver, you execute them with the permissions and as the user the webserver is running. You will be running them in the current working directory of the webserver too.

    All this leads to a number of possible reasons why things aren't working the way you want them to.

    The webserver might even be configured in a way not to let you use exec or other means to execute programmes locally.

    Best you can do is to find out which user the webserver is running with and which current working directory it is using. Change to that user id and working directory and try on the shell level. That helps avoiding a number of problems.

    The webserver's error_log file will tell you everything else, I suppose.

    Cheers, Sören

Re: Execute a perl program from a php web page
by injunjoel (Priest) on Sep 27, 2005 at 21:27 UTC
    Greetings all,
    Have a look here to learn about running Perl from PHP.

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
Re: Execute a perl program from a php web page
by aufflick (Deacon) on Sep 28, 2005 at 00:12 UTC
    Firstly, if your script doesn't absolutely have to be run as root, you should ideally use a different user and using root at all from a web script is a REALLY bad idea as various nodes and FAQs will tell you).

    sudo is good for this sort of thing - you can use sudo -u someuser perl foo.pl.

    I also assume you have set up your sudoers file correctly? First, your webserver userneeds certain permissions, but the normal setup would mean that sudo is blindly asking for a password - and there's noone there to type it in! The sudoers config file is very flexible. You will be able to specify what you want to allow in a very fine grained way. eg. "let the user apache execute the script /usr/bin/do_script.sh without a password".

    The sudo config format is, however, a bit tricky. Thankfully man sudoers gives you some examples of this sort of thing.

Re: Execute a perl program from a php web page
by Errto (Vicar) on Sep 27, 2005 at 22:51 UTC
    One thing I would suggest is changing the permissions on your server so that the web user has the ability to run the designated Perl script directly, without sudo. sudo won't work from a non-terminal-based script unless you use Expect or something similar because, by design, sudo requires the user to enter their own password interactively. sh1tn's solution will work, but unless I'm missing something, it's not a good idea because then any user on the machine can use this "Sudo" program to run any program they want as root, which would be disastrous on most systems.
Re: Execute a perl program from a php web page
by sh1tn (Priest) on Sep 27, 2005 at 21:45 UTC
    You can try with passthru funtion which saves some typing while testing and about sudo you can use some C wrapper with extended rights. Something like:
    #include <unistd.h> int main (int argc, char** argv) { char *prog = argv[0]; char *command = argv[1]; if(argc == 1){ printf("%s need command\n", prog); exit(1); } setreuid(0,0); system(command); }
    And then:
    #gcc -o Sudo Sudo.c #chmod 4755 Sudo
    In php (while testing):
    <? passthru("Path_to_Sudo Your_Perl_program"); ?>