Issues with exec() on php running a shell script - php

I'm trying to use a php script for executing a shell script. However I'm having some issues apparently with the web server.
I have a bash script called switch_audio.sh. It basically changes the active audio output of the system.
I also have a script.php that runs the code below:
<?php
echo "It's working";
exec("/var/www/html/switch_audio.sh");
?>
When I execute php script.php it's working fine. However, when I try to run it on the web browser by localhost/script.php I just get as ouput the "echo" part.
I've already tried to:
remove 'exec()' from the disable functions in php.ini;
give permissions for everybody in every folder on this path from "/" to the localhost folder;
Any thoughts about it?

Simple solution: exec() returns a string, but you also have to output it to the user:
<?php
echo "It's working";
echo exec("/var/www/html/switch_audio.sh");
?>

You already said you allowed the shell function on your php.ini configuration but it seems it's not working yet... so maybe:
You didn't restarted webserver service after changes
You have somewhere other statement more restrictive... maybe ini_set in your php files.
As a reminder, be sure that you are doing well on your php.ini file, check it for this kind of statement:
disable_functions=exec,passthru,shell_exec
Maybe you can try instead of exec other similar php function like shell_exec to check if it works. Or maybe is working and not showing anything.

Related

Execute a php script within a running php script

I'm trying to execute a php script from a php file, but it seems that i can't get the correct bin path of the php.exe. Its not doing anything as far as i can tell. Lets assume service-oauth.php is a simple echo.
EDIT3:
I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented.
EDIT2:
I tried calling the script directly from the server console as #Dagon suggested and it works, both using the php env variable and the php path to the bin, its clear that the path is correct, but something is preveting to get the output or to run the script using the php exec() function
service-oauth.php
<?php echo "Hello there"; ?>
And this is my script:
$basePath = plugin_dir_path( __FILE__ ); # Wordpress function, asume it works.
$fileToExc = $basePath . 'service-oauth.php';
# PHP_BINDIR: /usr/local/bin
# PHP_BINARY: /usr/bin/php
# exec("which php") /usr/bin/php
$phpPath = exec("which php");
$output = exec("$phpPath $fileToExc");
print_r($output);
There are a lot of answers on stackoverflow that recommend any of those 3 options i commenented in the code, but none of them seems to work, not sure if it is the path or something else that is not working. I've tested this script on my localmachine (windows) and it works (even though i had to use a hardcoded path to the bin since i wans't getting the correct path), but i'm testing on my production server (linux) and is not working.
Note: Let me be clear, that this is not a duplicate, i've tried the following answers in these posts and many others, and it didn't work for me:
How to get path of the php binary on server where it is located
Can't execute PHP script using PHP exec
PHP exec to run a file
How to call shell script from another shell script?
Execute a PHP script from another PHP script
I've also tried using .exe at the end of the binary(windows localmachine), using php-cli instead of php, and i've tested the excec function and it works, but not in this case. Also tried with a shebang in the called script.
It's propably something simple that i'm not aware of but i've been spending a lot of hours searching and testing and nothing so far. Any help is appreciated.
EDIT:
Using scandir on the bin folder shows
scandir("/usr/bin/")
Array(
...
[632] => php
[633] => php-cgi
[634] => php-cli
...
)
Tested if in safe mode using ini_get('safe_mode'), but it seems off.
I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented. – Zagen

PHP exec() not working on AppServ

I'm now facing the following problem. Please help me out.
I have two file in the same directory: test.php and test.r
test.php
<?php
exec("Rscript test.r");
?>
test.r
d=c(1:10);
write.csv(d, file="test.csv", fileEncoding="utf-8");
q();
While I access the test.php via chrome, it gives no test.csv.
Although it works fine on cmd Rscript test.r, it does not work while executing test.php.
Any suggestion?
Thanks
There are a few things to check:
File permissions. Set them to 0777 temporarily to see if it's working
Maybe the exec function is blocked. Check php.ini (or phpinfo()) for disabled_functions
Third options - is one you really always need to consider - an error in your script.
Happy debugging !
PHP/IIS will need to read/execute permissions to the command you are trying to execute with your exec() function.
Go through PHP documentation for exec() function article.
Hope, It will resolve your problem.

Run bash script to create file in PHP

In addition to my previous question, another problem appeared and I decided to make a new question for it:
I am currently calling a php script that than runs a bash script. The php script looks like:
chdir('/home/');
$output = shell_exec('./do.sh');
echo "<pre>$output</pre>";
The do.sh contains:
#! /bin/bash
echo 12;
dd if=/dev/urandom of=test.test bs=1048576 count=2
The problem is following:
When I call ./do.sh from the terminal everything works fine: test.test is created and the ouput is 12
However, when I call it from my php file, the output is 12 aswell, but no file is being created. Since I know almost nothing about bash scripting, I have no idea why this is happening...
Check if PHP safe_mode is enabled. You have to turn it off in your /etc/php.ini file, and obviously check filesystem permissions.

Can't run shell script from php web script

I am trying to run a shell script from a php script.
I have complete control of the environment (unix on mac), I should have all the permissions, etc. set correctly.
The web script is in /htdocs/
The shell script can be executed from anywhere so when I go to /htdocs/ in the shell, I can easily run it like this:
$ my_shellscript
.. but when my php script (which is located in htdocs) tries to call it:
shell_exec('my_shellscript');
I get nothing.
I have proven the script can be called from that location and I have temporarily granted full access to try to get it working somehow. I am going crazy, please help.
If you know of some other way of triggering a shell script via the web that would be fine.
Thanks in advance.
well i got few weeks same problem, the solution is to check if the apace has the permission to execute your script. You could also try to run the script in php cli.
Since it is a shellscript, it needs to be invoked with the path prefix. My guess is you need to do this:
shell_exec('./my_shellscript');
First thing: make sure php isn't running in Safe Mode
Next thing: Try running it with the exec() function and using the full path (e.g. /var/www/htdocs/my_shellscript)
Try doing
echo shell_exec('my_shellscript 2>&1');
which will capture the script's stderr output and print it out. If something inside the script is failing, this output would otherwise be lost when not being run interactively.

Problem with passthru on server

I have a problem trying to run passthru function in my php code (Joomla module). the code is following (this is only a snippet)
ob_start();
passthru("/usr/bin/whois 85.70.231.130 | /usr/bin/grep 'address:'",$code);
$whoisData = ob_get_contents();
ob_end_clean();
$whoisData = str_replace("address:", "", $whoisData);
$whoisArray = split("\n",$whoisData);
echo trim($whoisArray[1]);
when I run this on my localhost, it echoes what it should, but when I run this code on the production server, it echoes nothing and the $code variable contains 127 (command not found). I tryied add absolute paths to these commands into the passthru function, but it didn't helped. Interesting is, that when I run the code right from terminal via ssh and php command, it runs well, but when it's called from application context it doesn't. Does anybody know what I should to do?thanks
SOME EDITS..
safe_mode is on
webserver does not see into /usr/bin and /bin/ folders so what is the best way how to run these commands from php?
usr/bin/grep doesn't look like a valid path to a command.
The missing / at the beginning of the path to the second command might explain the command not found error... even if the first whois command is found.
Have you looked to see if your webserver / php is running chrooted?
print_r(glob('/*'));
if (file_exists('/usr/bin/grep') && file_exists('/usr/bin/whois')) {
print "maybe its a permissions thing?\n";
} else {
print "can't see executables required\n";
}
should give you a clue.
So I have already solved my problem with phpwhois library. I seems like with my server configuration is it unlikely that these functions will be working well. So thanks for your help:)

Categories