Unable to run a command from php to linux - php

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.

Related

Task not killing with exec() and posix_kill() in Ubuntu Server?

I'm setting a dashbord for manage a game server but I can not kill process with PID. I tried exec("kill $PID") exec("kill".$PID) posix_kill($PID, 15) but it did not work.
A posix_kill command give a "Operation not permitted" error yet the user apache is www-data and the user for the minecraft script is www-data also.
<?php
include('pidminecraft.php') ;
$output = shell_exec("bash /srv/scripts/pidphp.sh $PID ");
if ( $output == "run" )
{ exec('kill'. $PID); shell_exec("bash /srv/minecraft/minecraft_survie/start.sh >> /srv/web/minecraft/minecraft.txt 2>&1"); }
else
{ exec("bash /srv/minecraft/minecraft_survie/start.sh >> /srv/web/minecraft/minecraft.txt 2>&1"); }
?>

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 .

Running a .PY file from a PHP script

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

executing an ssh command in php via exec -ssh key issue

I have a bit of code that runs on the remote server fine but will not run on my local php instance.
private function serverCmd($server, $cmd, $exec = false) {
$line = system("whoami");
print($line."\n");
$localCmd = 'ssh -o StrictHostKeyChecking=no myuser#uk-staging.internal.myhost.com "mkdir -p /home/deploy/backups/etl; rm -f /home/deploy/backups/etl/*.gz; rm -f /home/deploy/backups/etl/*.csv; sudo chown myhost:mysql /home/deploy/backups/etl"';
for ($retry = 0; $retry < 10; $retry++) {
if ($exec) {
$output = array();
exec($localCmd, $output, $retval);
if ($retval === 0) return $output;
}
else {
system($localCmd, $retval)
if ($retval === 0) return true;
}
sleep(1);
}
return false;
}
The issue is with the this line : system($localCmd, $retval).
if i run the ssh command directly on my ubuntu shell it works but if php trys to run it via that line it just hangs and I get the following error on the server its trying to ssh into :
sshd[10203]: Postponed keyboard-interactive for my user sshd[10202]: error: PAM: Authentication failure for myuser.
My dev machine has ssh keys setup to that I can just ssh in without entering a password. I assumed that php would use the same as its using my user.
How do I make sure php is using the same keys and authentication methods to ssh in as my shell? This is a cli script
Anybody got any ideas?

How to get pid of the process that run in background exec by shell in php

I'm working on a shell in php, and I want to display the same output as bash. When in bash you execute sleep 10 & you'll get [1] <PID>. How can I do the same in php, when I call shell using:
if (preg_match("/&\s*$/", $command)) {
$this->$shell_fn($token, '/bin/bash -c ' . escapeshellarg($command) .
" > /dev/null");
return array(
'output' => '',
'cwd' => $path
);
}
$shell_fn is variable that point to wrapper over shell_exec, exec or cgi script called by curl. Is it even possible to get the pid from php or using a shell?
If you want the pid in bash, you can do use the ! special parameter to get the pid of the most recently backgrounded process:
bash -c 'sleep 10 & echo $!'
I don't know exactly how php spawns external processes, but I imagine you'd be able to capture the echo output here, just by running the above shell command.
I use this functions to manage process:
function ProcessStart($cmdline) // return pid
{
exec( "nohup $cmdline >/dev/null 2>&1 & echo $!", $output) ;
// print_r ($output);
return (int)$output[0];
}
function ProcessStatus($pid) // return TRUE (live) o FALSE (dead)
{
exec("ps -p $pid",$output);
// print_r($output);
return ( isset($output[1]) ? TRUE : FALSE ) ;
}
function ProcessStop($pid)
{
exec("kill $pid",$output); // kill -9 ??
}

Categories