How to execute bash script using php using Ec2 server [duplicate] - php

I have a bash script, that I run like this via the command line:
./script.sh var1 var2
I am trying to execute the above command, after I call a certain php file.
What I have right now is:
$output = shell_exec("./script.sh var1 var2");
echo "<pre>$output</pre>";
But it doesn´t work. I tried it using exec and system too, but the script never got executed.
However when I try to run shell_exec("ls"); it does work and $output is a list of all files.
I am not sure whether this is because of a limitation of the VPS I am using or if the problem is somewhere else?

You probably need to chdir to the correct directory before calling the script. This way you can ensure what directory your script is "in" before calling the shell command.
$old_path = getcwd();
chdir('/my/path/');
$output = shell_exec('./script.sh var1 var2');
chdir($old_path);

Your shell_exec is executed by www-data user, from its directory.
You can try
putenv("PATH=/home/user/bin/:" .$_ENV["PATH"]."");
Where your script is located in /home/user/bin
Later on you can
$output = "<pre>".shell_exec("scriptname v1 v2")."</pre>";
echo $output;
To display the output of command. (Alternatively, without exporting path, try giving entire path of your script instead of just ./script.sh

Check if have not set a open_basedir in php.ini or .htaccess of domain what you use. That will jail you in directory of your domain and php will get only access to execute inside this directory.

Related

run PHPunit from cmd file

I had create a cmd file:
phpunit Test.php > myTest.txt
It work when i run is directly. But when exec with PHP code:
exec("cmd.cmd");
a file myTest.txt created, but it blank
Most likely because phpUnit is not run when you call it via php (this could be due to security privileges).
Try
$output = '';
exec("cmd.cmd", $output);
echo $output;
And see what the execution actually returned, also you might need to specify a path since exec might run from the PHP execution path, not your PHP webroot where the file exists.

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 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.

How to change the terminal working directory with cli script?

I would like to change the directory in Linux terminal from cli script, not the PHP current working directory -- hopefully with shell_exec().
Ex: from user#host:~$ to user#host:/the/other/directory$
system() and exec() are not allowed.
This isn't working in my case:
$dir = '/the/other/directory';
shell_exec('cd '.$dir);
nor these
shell_exec('cd '.escapeshellarg($dir));
shell_exec(escapeshellcmd('cd '.$dir));
pclose(popen('cd '.$dir));
But shell_exec('ls '.$dir) gives me the list in that directory. Any trickery?
When you're executing several commands like you are doing:
shell_exec('cd '.escapeshellarg($dir));
shell_exec(escapeshellcmd('cd '.$dir));
It won't work. The first command has nothing to do with the second one, so you can't use the results of the 1st command to execute the 2nd command.
If you want to execute a chain of commands, use the pipe symbol | like:
echo shell_exec("ls /etc/apache2 | grep fileName");
Local changes made by shell_exec only apply to that php process and therefore ls would fetch from that other directory. But when you exit the php, you are back to bash who never really cares what process does.
I doubt you can change bash's current directory from outside.
If you need to run bash in other directory, you could try this inside your php:
system('gnome-terminal --working-directtory=/home/doom');
this will start another terminal in new dir but script will wait till you quit this shell.
If you mean you want to change the parent shell's directory by calling a php cli script, then you can't do that. What you could do is at best:
shell> eval `your/cli/script`
and in your script do something like
<?php
...
echo "cd $dir"
?>

Categories