I have the following code.
<?php
$command = '/usr/bin/mysqldump --user=marvin --password=mypassword --host=localhost opencart_status > /var/www/marvin/backup.sql';
$result = passthru($command, $report);
if(!$report){
echo "Error!";
}
else{
echo "OK";
}
?>
I tried to run the $command on putty in linux and everything is OK. But when I ran it in my php scirpt, the output was "OK" (echo "OK"), but no backup.sql was in my folder.
Related
i was trying run fast lane commands in terminal using php, the commands like cd, pwd, ls and chmod working fine in php using exec or shell exec functions but when i try to run fast lane command it throws error 127, how can i run fast lane using php?
function terminal($command)
{
$output = [];
$return_var = '';
//exec
if (function_exists('exec')) {
exec($command, $output, $return_var);
} else {
$output = 'Command execution not possible on this system';
$return_var = 1;
}
return array('output' => $output, 'status' => $return_var);
}
// $path = "cd /Applications/XAMPP/xamppfiles/htdocs/mystudiomobile/cordova7/platforms/ios/fastlane";
// $path_c = "fastlane init"; path and path_c present in test.sh
$command = "/Applications/XAMPP/xamppfiles/htdocs/php1/test.sh";
$path_change = terminal("$command");
if($path_change['status'] == 0)
{
echo json_encode($path_change['output']);
echo $path_change['status'];
}
else
{
echo "some problem";
echo $path_change['status'];
}
I found out that there are limitations in php to perform http and https protocols using system functions like exec. which could not run fast lane, the best practise is to go with bash script which access php instead of vice versa .
I have this PHP code:
ini_set('display_errors', 1);
ob_start();
passthru('/usr/bin/python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
$output = ob_get_clean();
echo $output;
$message = exec("/usr/bin/python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt");
print_r($message);
$command = shell_exec('python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
echo $command;
$output=shell_exec('python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
echo "<pre>$output</pre>";
$command = escapeshellcmd('python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
$output = shell_exec($command);
echo $output;
exec('sudo -u www-data python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
system("cd /usr/lib/cgi-bin && sudo python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt");
I would like PHP to:
1. launched the a.py script
2. returned the result which the console will display from a.py and display it in the web browser.
At the moment, nothing is showing up. I do not have any error message or warning.
Does anyone know what is wrong in the above code?
My server allows running scripts with the console
I want to execute bash command with script. If I echo the command, I get the proper response. But if I execute it from browser, it does not work. If I echo the command ls, it is executed and shown. I have granted all permissions. If I write command in terminal it works.
<?php
$banlista = $_POST['banlista'];
$ip = $_POST['ip'];
$command = "fail2ban-client set $banlista banip $ip";
$sporocilo = shell_exec("$command");
?>
It's better to run the command within the php code. You can get a response
from the actual command and verify that it worked.
If you have your code this way
<?php
$banlista = $_POST['banlista'];
$ip = $_POST['ip'];
$command = "fail2ban-client set $banlista banip $ip";
$sporocilo = shell_exec("$command");
?>
You can try to add this:
<?php
exec("sudo user /usr/bin/fail2ban-client set $banlista banip $ip", $output, $return);
echo "Failtoban client returned $return, and output:\n";
var_dump($output);
?>
You're probably missing a sudo and a user who has the right to run the
command from the browser.
I try to ping www.google.de with shell_exec and store the result into a variable but i get no result back from shell_exec.
<?php
$ping = 'sudo ping -c 4 ';
$url = 'www.google.de';
$command = $ping . $url;
$ping_result = shell_exec($command);
$datei = fopen("/var/www/myProject/result_ping","w") or die ("Could not open file!");
sleep(10);
if ($datei == false)
{
$ping_result = "Cannot open file!";
}
else
{
fwrite ($datei , $ping_result);
fclose ($datei);
}
echo $command; //Output: sudo ping -c 4 www.google.de
echo $ping_result; //Output: nothing
?>
The file result_ping has all rights (chmod 777).
Maybe the webserver is not allowed to execute ping?
Add 2>&1 to your command to ensure you're not getting an error message that shell_exec would filter off:
$command = $ping . $url . ' 2>&1';
shell_exec will return NULL in case of error. With that modification you redirect any error message to normal output, thus forcing shell_exec show every message you would normally get on a console session.
I try to execute this PHP command :
$result = exec("sudo python /home/pi/test.py",$output, $ret);
var_dump($result);
echo "<br>";
var_dump($output);
echo "<br>";
var_dump($ret);
This command works perfectly on a Linux Terminal but with the PHP it doesn't work.
Here the result on the PHP page :
string(0)
array(0) { }
int(9)
I verified the process with ps -ef, nothing appears.
It might be help someone with the same problem.
I managed later to execute a Python script with this PHP code :
$command = escapeshellcmd('sudo /home/pi/test.py');
$output = shell_exec($command);
echo $output;
For the moment, I never succeed to execute this in background. I try those solutions :
$command = escapeshellcmd("sudo /home/pi/test.py >/dev/null &");
$command = escapeshellcmd("sudo /home/pi/test.py &");
My PHP page wait also the end of the process, but i don't want to.