Running a .PY file from a PHP script - php

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

Related

Unable to run a command from php to linux

I was trying to run a command from PHP to my Linux server but I'm having some trouble with one command:
This is the script:
<?php
$output=null;
$retval=null;
exec('speedtest -L -f, --format=json 2>&1', $output, $retval);
echo "Returned with status $retval and output:\n";
print_r($output);
?>
The output is:
Returned with status 134 and output:
Array ( [0] => terminate called after throwing an instance of 'std::logic_error'
[1] => what(): basic_string::_M_construct null not valid
[2] => Aborted (core dumped))
The user that runs the script is: www-data
if I try to run it directly on the terminal it works.
command from terminal: sudo -u www-data speedtest -L
output: {"type":"serverList","timestamp":"2021-02-23T09:20:51Z","servers":[{"id":4302,"name":"Vodafone IT","location":"Milan","country":"Italy","host":"speedtest.vodafone.it","port":8080},{"id":7839,"name":"Fastweb SpA","location":"Milan","country":"Italy","host":"spd-pub-mi-01-01.fastwebnet.it","port":8080},{"id":11427,"name":"EOLO","location":"Milan","country":"Italy","host":"test.eolo.it","port":8080},{"id":1434,"name":"CWNET","location":"Milan","country":"Italy","host":"speedtest.cheapnet.it","port":8080},{"id":3667,"name":"TIM SpA","location":"Milan","country":"Italy","host":"speedtestmi1.telecomitalia.it","port":8080},{"id":8211,"name":"CDLAN S.R.L.","location":"Milan","country":"Italy","host":"speedtest.cdlan.it","port":8080},{"id":11675,"name":"Italiaonline Supernap","location":"Milan","country":"Italy","host":"speedtest-supernap.italiaonline.it","port":8080},{"id":20551,"name":"Optima Italia","location":"Milan","country":"Italy","host":"mi-speedtest.optimaitalia.com","port":8080},{"id":26415,"name":"P-Lab","location":"Milan","country":"Italy","host":"speed.speedymilan.net","port":8080},{"id":19177,"name":"Seeweb","location":"Milan","country":"Italy","host":"ookla-mil.seeweb.it","port":8080}]}
If I run another command it works perfectly:
<?php
$output=null;
$retval=null;
exec('whoami', $output, $retval);
sleep(1);
echo "Returned with status $retval and output:\n";
print_r($output);
?>
The output is:
Returned with status 0 and output: Array ( [0] => www-data )
Do you guys have any suggestions?
I figured out a way to make it work but probably isn't the best one:
<?php
// attributes
private $output;
private $retval;
private $command;
// methods
function get_serverUpdates($user, $password) {
$command = 'echo '.$password.' | su -l '.$user.'-s /bin/bash -c "speedtest -L -f, --format=json" 2>&1';
$resl = exec($command, $output, $retval);
return json_decode(substr($resl, 10));
}
?>
It does return the correct information.
I think the problem was that as #MarkusZeller said that www-data doesn't have the permission to run all the programs installed.

How to run Fast lane in terminal using php? exec() or shell_exec()

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 .

Executing bash with PHP

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.

mysqldump - right command wrong execution

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.

Execute Python with PHP

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.

Categories