View Full Version : call perl script from php
02-18-2010, 04:27 PM
i'm trying to call a perl script from php without success.
my php code :
$tel = $_POST['Tel'] ;
$message = $_POST['message'] ;
//$tel = 'xxxxxxx';
//$message = 'viwwwww';
$outpout = exec("perl /var/www/htdocs/msg.pl $tel $message");
however, when i enter the following command in the command prompt it works
can someone help me please !!
thank's in advance
02-18-2010, 04:43 PM
Do you have execute privileges on you're server for php?
Test with a simple:
Also, you'll be wanting to search a little bit on how to correctly handle exec calls in PHP. What you have there is a huge risk since you're completely trusting you're users.
02-18-2010, 06:43 PM
Do you have execute privileges on you're server for php? yes i do
Test with a simple:
i have tested your code and it return //
02-18-2010, 09:50 PM
Thats an odd output, I would have expected a id of some sorts to show up. Oh well.
You said that you can execute /var/www/htdocs/sendsms.php from the cli correct? I presume thats either as you're user or a root user.
Can you check the file permissions on /var/www/htdocs/msg.pl? If executing the entire php script from the cli works, but not from apache, that leads me to believe there is a permission problem when executing /var/www/htdocs/msg.pl. It needs to have execute for world or be owned/grouped by you're apache user (nobody maybe?) and executable by them for it to work in exec.
If you give you're exec an array variable (its by ref so make sure its an actual variable), you can capture the entire output into the array line by line, and a third parameter should have the return result (assuming 0 is what you're wanting). That may give you some direction when it comes to what the issue is.
You can also check if the file is executable with the is_executable() function.
Aside from that, I won't be able to give you much help I'm afraid. Hopefully it lies with either having difficulty finding the perl command (which I doubt), or an issue with permissions.
02-22-2010, 01:02 PM
I'm the root. and as i've mentioned before i can execute /var/www/htdocs/sendsms.php from the cli but not from the web
The file permissions on /var/www/htdocs/msg.pl are 755 .
i've tried this code, from the cli the output is /var/www/htdocs/sendsms.php is executable, from the web i have /var/www/htdocs/sendsms.php is not executable
$file = '/var/www/htdocs/sendsms.php';
echo $file.' is executable';
echo $file.' is not executable';
i've also tried this code,from the cli the output is root from the web the output is "OS Error: 127."
if($error) echo ("OS Error :$error.<br/>\n");
Concerning my server (apache), in the httpd.conf i have
maybe that switching to apache so changing the user cause problems ?
thank you anyway !
02-22-2010, 04:05 PM
yes, I'd suspect it is related directly to the use of apache or ini configurations.
The permissions are fine, so we know thats not the issue. With the web based envorinment and what error's its tossing at you (fnf I believe), my assumption at this point is either safe_mode is enabled (check with printf("Safe mode enabled: %d\n", ini_get('safe_mode'));), or that the path is not accessible to to a chroot jailed user. I believe you'll need to change the www account to /bin/sh for its shell.
02-23-2010, 11:33 AM
:thumbsup: the probleme was coming from the configuration of apache in Openbsd. . the server is chrooted by default so he doesn’t run any programs.I just disabled the chroot and it worked.
I still have another question, my perl script doesn’t work entirly, some commands in the script need root user to be executed , but i’m www user , can i change the user using a command or script in php.
02-23-2010, 05:36 PM
Using PHP, I don't think that will work. You're perl script itself can probably select a new user to run with, but if you're requiring a root user to execute the script, I would not be public fronting the script to start with. That will just open you up to potentially dangerous security holes.
I suppose you can su from you're perl script, but I still think you're really opening it up to problems. PHP should be also able to su you're script prior to calling the pl. I assume that would also work.
What is it you're requiring root privilege for?
02-24-2010, 11:30 AM
i wich to sendsms,the connection to gsm need root privilège.
i have tried calling the comand sudo in my php code, but it doesn't work
exec("sudo perl /var/www/htdocs/msg.pl $tel $message ");
i’ve also modify the sudoers file ( incomment the line %wheel ALL=(ALL) NOPASSWD:ALL) and i’ve added the user www to the group ‘wheel’
:confused: I must have forgotten something!! I'll try again with php before switching to perl
thank you again for your help
02-24-2010, 03:40 PM
Yeah, let me know how it all turns out.
I've never needed to su to execute any external script before, so I'm not certain what the issue is.
02-25-2010, 11:54 AM
Great thanks for your contribution to this thread,i wouldn't have been able to solve it without your help and your suggestion.
i have solved my problem using command sudo in my php code.
exec("sudo perl /var/www/htdocs/msg.pl $tel $message");
i just gave to 'www' user root privilege and his own shell (this is what was missing,because,in openbsd 'www' doesn't have shell)
# userinfo www
groups www wheel
gecos HTTP Server
# userinfo www
groups www wheel
gecos HTTP Server,,,
for more details " http://www.daemonforums.org/showthread.php?p=30383 "
02-25-2010, 03:36 PM
Great, I figured the shell was a big issue for sure (suggested that a few posts up hah).
Once you change the shell, do you still need to sudo for it?
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.