Run bash script to create file in PHP - 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.

Related

Why does this PHP script with a shell_exec call run from the command line in Windows 10, but not the browser/localhost?

I'm using XAMPP on Windows 10, and trying to run a simple PHP script that uses the shell_exec function to call a simple R script in the same directory. Here's the PHP index.php file:
<?php
echo shell_exec('Rscript test.R');
And here's the test.R file:
print("Hello, World!")
From the command line (i.e., Git for Windows), when I run php index.php in the folder, I get the following output:
[1] "Hello, World!"
However, when I run index.php from the web browser via XAMPP and localhost, I don't see anything on the page. At first, I thought it might be a permissions issue, so I gave Full control access to all users on the folder, but it didn't seem to change anything.
Any ideas on how to get this working from the browser? Thank you.
Found the answer to the problem I was having. Apparently, even though Rscript is part of my Windows 10 PATH variable, when I try to execute an R script via shell_exec in PHP, I have to have the whole path to the Rscript executable (I guess because the PHP script / Apache server doesn't know about the path variable).
I changed the shell_exec function call as follows, and it worked:
echo shell_exec('"C:\Program Files\R\R-4.1.0\bin\Rscript" test.R');
Here's a link to the SO post that helped me figure this out:
shell_exec does not execute from browser requests

php exec() return error when calling external program (postmap)

I'm trying to update the postfix databases inside a php code (Apache linux based server)
I tried to use the shell_exec() and exec() command like this:
$out =shell_exec('postmap /etc/postfix/virtual; echo $?');
var_dump($out);
While postmap /etc/postfix/virtual works in the command line, I'm have a 1 error code when calling it like this inside my php and it does nothing.
/etc/postfix/virtual is in chmod 666 and /usr/sbin/postmap -rwxr-xr-x
"Of course" it works when I'm calling the php as a cli.
Other programs like (ls or echo) works.
Do you have any idea why it doesn't works?
Problem solved:
1. To check where the problem exactly come from (I was guessing it was a privilege problem), I redirected STDERR to STDOUT so I can debug clearly the code.
exec('postmap /etc/postfix/virtual 2>&1',$output);
Then I learned that postmap is looking for /etc/postfix/virtual.db and not /etc/postfix/virtual and had no right to write.
Setting the file chmod as 666 fixed the problem.

Issues with exec() on php running a shell script

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.

executing a script via php exec which creates a file on the server

I have a php script which calls a shell script as below -
#!/bin/bash
timestamp=$(date +"%d-%m-%Y %H:%M:%S")
echo $timestamp >> results
The php script -
<?php
$mycmd = exec('/bin/bash exectest.sh',$op,$er);
var_dump($mycmd);
var_dump($op);
echo $er."\n";
?>
The php script returns error code 1 for $er but when i tried to modify the shell script to just print instead of writing to a file. the Php script then returns 0 and succeeds.
Any ideas of where I need to fix this?
I have tried giving the full path for the script and also this is the same case when i tried using a python script in place of a shell script.
Your observation indicates that this is most likely a permission problem, e.g. the user running PHP does not have write permission to either the results file in its current working directory or the directory itself (if the file does not exist yet).
It happened to be running on an AFS machine hence required afs priviledges for httpd.
fs sa . http write
sorted the issue.

Run php in command line on CentOS7

I expected this to be quite simple but I cannot make it work.
I'm trying to run a php script in command line (to finally be able to automate it in a cron.)
The file is outside of the public web folder to avoid executing it from a webbrowser.
File is called 'daily.php' and contains :
#!/usr/bin/php
<?
echo "hello \n";
file_put_contents("daily.log", "executed...", FILE_APPEND | LOCK_EX);
?>
then in my terminal I run (as root):
/usr/bin/php daily.php
but it simple outputs me the full source code without the hashbang.
So I tried changing the file to 755, tried to chmod +x it but still outputs me the full source code.
I had a look in the man page and I found :
-f <file> Parse and execute <file>.
Tried this but still the same output.
Why is this? how can I interpret this file?
Ok
I found the answer after 2 hours of trouble, and just after posting this question.
Even if you hashbang the file, even if you call php directly, you need to implicitly tell it is php by using :
<?php
?>
and not
<?
?>

Categories