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!
Related
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
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
I'm trying to execute a command through PHP with shell_exec. The PHP file is hosted by Apache on my Ubuntu server.
When I run this:
echo shell_exec("ps ax | grep nginx");
Then I get to see data. But when I run another command, for example:
echo shell_exec("cat /usr/local/nginx/config/nginx.config");
Then it's not showing anything at all. But when I copy that command and paste it in my terminal, then it executes fine.
My Apache server is running as user www-data. So I edited sudoers and added this line:
www-data ALL=(ALL:ALL) ALL
I know this is a security risk, but I wanted to make sure (for now) that www-data is able to execute all commands. But, for some reason I'm still not able to execute all commands with my PHP script.
Anyone any idea what to do?
have you read http://php.net/manual/en/function.shell-exec.php
There is quite a discussion in comments section. Top comment is:
If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:
Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
In the above example, a line break at the beginning of the gunzip output seemed to prevent shell_exec printing anything else. Hope this saves someone else an hour or two.
echo shell_exec("sudo cat /usr/local/nginx/config/nginx.config");
Try that.
so I have read about 10 answers and everyone seems to suggest ideas which for some reason don't work.
i am trying to execute a simple command line which is "svn update" but it is not working and it returns NULL
so i have tried trial and error the way and for now this is what i can say;
i have tried several commands like
<?php
exec ("cmd /c ping 127.0.0.1 -n 1 > results.txt ");
?>
and
<?php
exec ("cmd /c chdir > results.txt ");
?>
and both work.. infact chdir says the exact position where the php file executing the line is stored on the pc..
so the problem now is, why do some commands like this:
<?php
exec ("cmd /c dir > results.txt ");
?>
don't work? this results and empty value even though inside the folder i have several files and directories.
and why if i use the command prompt to move into the folder where the php file is store and type svn update it works and doing
<?php
exec ("cmd /c svn update > results.txt ");
?>
return a NULL?
any help is really appreciated.
it feels like i have some restrictions dued to the configuration setup because when i try in local using apache i can get most of the commands to work (shell_exec, system, exec, even without the cmd /c)
Ok.
i have managed to solve the issue..
this is what i did:
first check exactly what username is running for the specific website.. to do so do:
<?php
$out = array();
exec('cmd /c whoami 2>&1',$out,$exitcode);
echo "<br />EXEC: ( exitcode : $exitcode )";
echo "<hr /><pre>";
print_r($out);
echo "</pre>";
?>
this will return the computername followed by the username..
now on the computer running the webserver run
control userpasswords2
and give administrator powers to the username whoami said
this will allow you to finally run any command you want using exec or system_exec
on the other hand continuing with my SVN command i found out that I had another problem which is that when you run it, it will look for the config file which is under the administrator account and will give an error saying:
svn: E125001: Can't determine the user's config path
to solve this issue you simply have to specify in the command the config_dir by doing this:
exec('cmd /c svn update --config-dir C:\Users\Administrator\AppData\Roaming\Subversion C:\\inetpub\\vhosts\\websitename\\httpdocs\\folder 2>&1',$out,$exitcode);
hope this helps others which are having problems like the ones i had!
This is likely a system user permissions issue. I tried your example:
<?php exec ("cmd /c dir > results.txt "); ?>
On my Windows7 with Xampp installed and it worked perfectly fine. However with IIS the "user" may not have permissions to the directory, off the top of my head I think it may be the system user IIS-IUSR or something like that.
Here is a link that might help with user permissions for IIS: http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Okay, based on your answers, I think you should try this:
Execute your command using the 'svn.exe' executable (replace the [[reposity location]]).
It is possible that your client has another svn.exe location, but you will have to figure that out yourself :)
exec('cmd /c "c:\\Program Files\\TortoiseSVN\\bin\\svn.exe" up "[[repository location]]"');
What happends now?
There is also a second parameter in exec, maybe you should also take a look at that one.
The only thing that helped me was, complete routes:
$template_file = "C:/archivos/archivos.tex"
$cmd = sprintf("C:/texlive/2020/bin/win32/pdflatex.exe " .$template_file );
$result = exec($cmd, $output, $a);
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!