Hi I am trying to make use of exec command and change the directory to execute a specific command, but change directory is not working
when i execute exec('whoami'); i get proper output
when i execute following code
<?php
ini_set('error_reporting', E_ALL);
$var = exec('cd /root/');
echo exec("pwd");
?>
my directory is not changing to root
I think you need to use chdir() rather than exec('cd /root/'):
<?php
ini_set('error_reporting', E_ALL);
$var = chdir('/root/');
echo exec('pwd'); // Prints /root
This may not work if this is running on a website and the user the HTTP server is running as does not have access to /root.
The reason that exec() doesn't work is that it opens a new subprocess each time. Changing the current directory in one does not affect others spawned after.
Related
I cannot get a button on my webpage to execute a python3 script that is also inside the same folder in the server as the rest of the html and php files.
exec();
system();
escapeshellcmd();
shell_exec()
all of these commands are not working for me right now. I have chmod +x my .py file and included #!/usr/bin/env python3 at the very beginning of my python file.
<?php
if( isset($_POST['runScript'])){
$command = escapeshellcmd('/nfs/nfs7/home/team51/cgi-
pub/dataProcess.py');
$output = shell_exec($command);
echo $output;
}
?>
<form method="post">
<input type="submit" name="runScript" value="runScript">
</form>
In the end the python script should place a csv file in the same folder as the rest of the files. But I am getting nothing.
Some functions just print back "Array()"
You are running pythin script from apache user. Python path may not be accessible by apache user
Use full python installation path to execute script.
exec("/usr/bin/python /nfs/nfs7/home/team51/cgi-
pub/dataProcess.py");
To find out exact error check apache error log. If error logs give permission denied , it means apache is not having permission to execute script.
You can get access from sudoers file.
Try Make a sh file to execute the python script and use PHP to execute this sh file
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.
I'm having a weird problem. I have installed ffmpeg on my server. When I run "ffmpeg" from SSH (PuTTY) it confirms the install:
Then I placed the following code in a PHP file, inside a website on the same server:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$output = exec('ffmpeg -h');
echo "<pre>$output</pre>";
?>
However when I run the PHP script, the page is blank, there is no output. I can confirm that exec is working fine, because when I run php -v | grep cli both in PHP and in terminal, they both output the same thing.
I am using Plesk (web host edition) to manage the site, and have given it access to the server over SSH (/bin/sh)
What am I missing here?
You want to use the full path to ffmeg as it's likely not in the webserver user's path environment variable.
Also, the return of exec() is the last line of output, which may be a blank line, you never know. So use the second parameter to capture all output in an array:
exec('/path/to/ffmpeg -h', $output);
echo implode('<br>', $output);
Or you can try system() or passthru() to output directly:
echo "<pre>" . system('/path/to/ffmpeg -h') . "</pre>";
I would recommend trying
$command = escapeshellcmd('/root/bin/ffmpeg -h');
$output = shell_exec($command);
echo "<pre>$output</pre>";
EDIT - ADDITION
However, the issue in this case is that files in /root are not available to the web server user for security reasons. The ffmpeg executable should go into /usr/local/bin or another directory where it is available to the web server user.
I am trying to use a PHP script to call a shell script and print the output to the browser.
I have confirmed that all permissions settings are set correctly, both the PHP and the bash script have full control.
If I run the shell script in the current directory (same directory as the PHP file) then it works fine. But if I try to run the shell script from /some/other/directory/shell.script, then it does not work. Why is this so?
I have tried to chdir() to the other directory, but after running a getcwd(), it never changed directories.
I have also tried the exec command by itself and it does not work.
<?php error_reporting(E_ALL); ini_set('display_errors',1);
echo "<h1>EDS Count Report</h1><p>";
$output=shell_exec('cd /app/script/catalog/ && ./EDScount_report.sh');
echo $output;
?>
contents of SHELL script:
#!/bin/bash
echo "this is a test!"
Try this please
<?php
$output=shell_exec('sh /path/to/otherdirectory/shellscript.sh');
echo $output;
?>
if you are certain that you can access that dicertory , you can use also this:
<?php
$output=shell_exec('cd /path/to/otherdirectory/ && ./shellscript.sh');
echo $output;
?>
I'm trying to run a shell script from a php frontend
heres the shell file (run.sh chmod to 777)
#!/bin/bash
wget -O index.html http://markets.usatoday.com/custom/usatoday-com/html-mktscreener.asp
python hw7-9.py index.html
echo "done";
Heres the php front end
<?php
$output = shell_exec("run.sh");
echo "<pre>$output</pre>";
?>
But it php page doesn't return anything except
<pre></pre>
Have you tried doing error_reporting(-1); at the top of your PHP script. Likely shell_exec is disabled on your server.
Would you try
$output = shell_exec("sh run.sh");
Check what user the php/web server is actually run as - e.g. "www-user" may have no permissions whatsoever to do the things your script is trying to do (and for good reason).