Use php to execute a command in terminal - php

Hello Im trying to create a php that executes a command in terminal. I dont know why but my code does not work.
When I go to ..../api.php?link=hello
it does not create the folder with that name
<?php
$parameter = $_GET['link'];
$output = shell_exec('sudo mkdir $parameter');
echo "<pre>$output</pre>";
?>
Fixed my command was not running because I had to put "sudo mkdir $parameter" not 'sudo mkdir $parameter' because my $parameter had symbols that confused the command. Thanks everyone for helping me figuring this out!

According to the documentation of shell_execute (https://www.php.net/manual/en/function.shell-exec.php), You need to disabel safe mode.
On linux servers
Run the following command:
`vi /etc/php.ini`
Go to the line safe_mode = on and press the "i" key.
Change the line to safe_mode = off and press the "Esc" key.
Type :wq! to save your file.
On Windows Servers
Open c:\windowsphp.ini in Notepad.
Change the line safe_mode = on to safe_mode = off.
Save and close php.ini.

Generally speaking , to problem is due to the permission , so here is the solution :
Let's suppose your file php that contain the exec command exist inside a folder nammed src
src/api.php
The src folder should have as owner www-data:www-data instead of something else to make php able to exec command .
$output = shell_exec('sudo mkdir $parameter 2>&1');
Add 2>&1 to see the error occuring inside .
When modifying the permission , you can exec the command, but be ware
that the file or folder yu are creating should be inside the src
folder , if it's another place , the parent folder should have as
owner www-data:www-data

Related

Using exec() in php via web

Hello i was trying to use exec() in php via web page to execute a file in the root directory, and for some reason none of my commands work except
ls
witch even when i do
ls /root
it doesn't work seems i can only do ls in the current directory or commands like whoami which returns apache
i've tried setting user permissions for apache, and i've tried setting permissions for file or /var/www/html directory and nothing seems to work any ideas?, my basic code below
<?php
command = "ls /root";
exec($command);
?>
The exec command return only the last line of your command.
Like said in the documentation, you should add a second parameter to get the result or use the function passthru

Run linux command from web page

I am trying to run command from a web page (php) to my linux server. My php page look like this: $old_path = getcwd();
chdir('/var/www');
$output = shell_exec('./test.pl');
chdir($old_path);
test.pl is a simple test file that create a txt file.
If I run the command php /var/www/page.php everything works fine, but when I request the page in the browser nothing happen.
Thanks !
Make the owner of the file the apache/php user www-data:www-data
sudo chown www-data:www-data /var/www/test.pl
Give the group and others the right to execute it
chmod go+x /var/www/test.pl
Then put the absolut path into shell exec
$output = shell_exec('/var/www/test.pl');
var_dump($output);

How to fix PHP (shell_)exec() permissions for copy pasting?

The following command executed in PHP's shell_exec() function:
echo 'A' ; cp -v $original $destination ; echo 'B' ; whoami
Will output ABapache, but nothing else. The copy command does not seem to be working. ($original and $destination are replaced with the correct path).
I made the apache user the owner of the folders I'm using for the cp command. It still doesn't work, I tried chmodding and more, nothing seems to work.
Is it blocked within PHP to run the cp command?
Thanks!

Executing .exe file using PHP on Linux server

I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script
hello bash script:
#!/bin/bash
./program.exe file.mp4 // file.mp4 is an an input for .exe
echo "Hello World!"
shell.php:
<?php
$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>
Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.
I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.
Update:
I ignored the shell script and I used
<?php
$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );
?>
also I granted access to program.exe
chmod 777 program.exe
the error I receive in the browser :could not open debug.bin!
use the absolute path to hello executable exec("sh path/to/the/file")
I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.
For any of this to work properly wine needs to be already installed.
On Ubuntu systems try:
sudo apt-get -y install wine
You then need to know the owner of the web server process. If you are running the apache web server try the following:
cat /etc/apache2/envvars | grep "RUN"
The output will look something like this:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:
sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe
Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.
<?php
$output = shell_exec("wine /var/www/program.exe file.mp4" );
?>
Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.
It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.
Try using the absolute path, such as exec("sh /path/to/file")
Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.

php command line exec() multiple execution and directories?

I am trying to Execute a multiple commands in php using exec() and shell_exec but i am getting a null value back which i shouldn't and nothing is happening (if i copy and paste the strings below in the command line it will work fine and accomplish the job needed) this is the commands i am using:
$command = "cd /../Desktop/FolderName;";
$command .= 'export PATH=$PATH:`pwd`;';
$command .= 'Here i execute a compiler;';
and then i use the escapeshellcmd()
$escaped_command = escapeshellcmd($command);
then
shell_exec($escaped_command);
any ideas what i am doing wrong and i also tried escapeshellarg() instead of escapeshellcmd()?
Solution: the Problem was the permission of the execution compiler for other owners is non and this was the problem.
because when you are using exec() function in php the owner of the file will be www-data so you need to give permission for the www-data either from the ACL of ubuntu or whatever linux based operating system(you can know the owner by doing this exec('whoami')), or by the files you need to execute.
(Sorry my bad English)
On Linux you can add your Commands in a Shell Script.
You can put this in any file:
#!/bin/bash
cd /../Desktop/FolderName
export PATH=$PATH:`pwd`
EXECUTE COMPILER
And save this as fille.sh
Then, add execution permissions:
chmod +x path/to/file.sh
From PHP, you can call this Script executing:
shell_exec('sh path/to/file.sh');
Hope this helps!

Categories