How to pass a qsub command to ssh2_exec in PHP? - php

I have a problem with putting a "qsub" command via "ssh2_exec" in PHP.
When I am running a simple test it works very well:
require_once('/apps/phpPackages/vendor/autoload.php');
use phpseclib3\Net\SSH2;
$ssh=new SSH2('xxxxxxxxx');
if (!$ssh->login('xxxxx', 'xxxxxxx')) {
exit('Login Failed<br>');
}else { echo "SSH Connection established<br>"; }
echo $ssh->exec('echo "Hello World" > test.sh');
But when I try to run a qsub command, the qstat command shows the job is running but not stopping and the command is not executed at all.
require_once('/apps/phpPackages/vendor/autoload.php');
use phpseclib3\Net\SSH2;
$ssh=new SSH2('xxxxxxxxx');
if (!$ssh->login('xxxxx', 'xxxxxxx')) {
exit('Login Failed<br>');
}else { echo "SSH Connection established<br>"; }
echo $ssh->exec('qsub list_of_command.sh');
If I run directly on the terminal qsub list_of_command.sh, the job is executed and I have the results needed
Any idea why qsub is not working properly through SSH2?

Related

Passing a qsub command thru ssh2_exec in PHP

I have a problem with putting a "qsub" command via "ssh2_exec" in PHP.
when i am running a simple test it works very well:
require_once('/apps/phpPackages/vendor/autoload.php');
use phpseclib3\Net\SSH2;
$ssh=new SSH2('xxxxxxxxx');
if (!$ssh->login('xxxxx', 'xxxxxxx')) {
exit('Login Failed<br>');
}else { echo "SSH Connection established<br>"; }
echo $ssh->exec('echo "Hello World" > test.sh');
but when i try to run a qsub command, the qstat shows the job is running but not stopping and the command is not executed at all.
require_once('/apps/phpPackages/vendor/autoload.php');
use phpseclib3\Net\SSH2;
$ssh=new SSH2('xxxxxxxxx');
if (!$ssh->login('xxxxx', 'xxxxxxx')) {
exit('Login Failed<br>');
}else { echo "SSH Connection established<br>"; }
echo $ssh->exec('qsub list_of_command.sh');
if I run directly on the terminal qsub list_of_command.sh
the job is executed and I have the results needed
Any idea why qsub is not working properly through SSH2 ???

shell_exec not giving any result, and not doing anything

So I have question which in my head should seem very simple to solve.
I want to ssh to a server, which I have done a ton of times, and then make a shell execute which I have done a ton of times as well, but it is not working.
The code i am using
<?php
$ip = '1.2.3.4';
$cmd = "ssh user#".$ip;
$result = shell_exec($cmd." 'sudo /bin/systemctl stop wildfly.service'");
echo "<pre>output: $result</pre>";
echo "<div class='alert alert-success'><strong>SUCCESS</strong><br>Wildfly node has now restarted</div>";
?>
Running the command directly from the terminal
ssh user#1.2.3.4 sudo /bin/systemctl stop wildfly.service
It works, but running it within php gives me nothing, and it not doing anything.
Can someone maybe guide me to what I am doing wrong with my shell_exec?
Thanks in advance!
function execPrint($command) {
try {
$result = array();
exec($command.' 2>&1', $result);
foreach ($result as $line) {
print($line . "\n");
}
echo '------------------------' . "\n" . "\n";
} catch (\Exception $e) {
print($e);
}
http_response_code(200);
}
i made this function to get result
add 2>&1 in last of the CMD
use print with every line
use try and catch to catch any error
The user attempting to execute those shell commands from php is likely _www and not you. Try this code in your php to gain insight:
$shellscript = 'whoami';
$sr = shell_exec($shellscript);
echo '['.$sr.']';
Make sure the shell_exec function is not disabled. It usually is disabled by default in CPanel accounts PHP.ini and PHP-FPM .ini files.
You can check it using this validation
if (is_callable('shell_exec') && (false === stripos(ini_get('disable_functions'), 'shell_exec'))) {
echo "shell_exec enabled";
} else {
echo "shell_exec disabled";
}
It's the most common reason i've found for shell_exec to return always empty
You can also execute a quick command for testing purpouses
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
echo "Command: shell_exec('ls -lart')";
try {
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
} catch (Exception $e) {
echo $e->getMessage();
}

PHP: Cant start a Server using shell_exec()

I want to create my own little control panel for my minecraft server,
I got problems with executing commands in php.
So if I want to execute a .sh file in PHP It works, I tried to do an output with
echo "xyz"
This works, but if I try to start a server, it doesnt..
My Server-Start-File:
echo "$1 's Server is starting with $2 MB of RAM"
screen -AmdS $1 java -Xmx$2M -jar spigot1649.jar
My PHP-Script:
<?php
$x = shell_exec('./start.sh test 512');
if($x) {
echo "Started";
echo "" . shell_exec('./start.sh test 512');
} else {
echo "Failed";
}
?>

Calling Matlab exec with argument(s) from PHP doesn't work

My first time to deploy matlab exec in php and i need ur help.
I have a matlab script compiled as sampleExe.exe (standalone app) with a single argument 'IdNo' to processes images. When i call it thru command line using sampleExe 2014000, the program runs and gives the desired output. However, I am having trouble when deploying/calling sampleExe.exe file from php as it gives me no output at all. :(
Here's the code i tried based on this: Call matlab exe from php is not working well
<?php
define("EVAL_IMAGE","sampleExe.exe");
$target=isset($_REQUEST['IdNo'])?trim($_REQUEST['IdNo']):"";
if($target==""){
echo "No folder name is passed";
exit();
}
passthru(EVAL_IMAGE." ".$target);
?>
Any help is very much appreciated. Btw, I tried running it in a localhost and sampleExe.exe is also save in c:/wamp/www
<?php
try {
define("EVAL_IMAGE","mainProg1.exe");
$target=isset($_REQUEST['IdNo'])?trim($_REQUEST['IdNo']):"";
if($target==""){
echo "No folder name is passed";
exit();
}
set_time_limit(300);
$return = exec(EVAL_IMAGE." ".$target);
echo "return = " .$return;
}catch (Exception $e) {
echo 'Message: ' .$e->getMessage();
}
exit(0); ?>

Run Scripts from PHP

I have a question about interaction PHP and Linux system.
I want to launch Steam from WebPage, and address of this page is
http://site.eu/cp/user/services/21/steaminstall
And internal path to WebPage from where im running command is
/var/www/wwwuser/data/www/site.eu/panel.php?(here goes $_POST[''] data)
and the Steam Client is
/var/www/wwwuser/data/Steam/SteamInstall
The thing is, i know how to access this script, also i can execute simple
echo "Script is Runing"
But when my actions comes to this code
#!/bin/bash
wget -P /var/www/aftersoft/data/Servers/Steam/ http://media.steampowered.com/client/steamcmd_linux.tar.gz &&
tar xvfz /var/www/aftersoft/data/Servers/Steam/steamcmd_linux.tar.gz -C /var/www/aftersoft/data/Servers/Steam/ &&
sh /var/www/aftersoft/data/Servers/Steam/steamcmd.sh +login anonymous +quit &&
rm /var/www/aftersoft/data/Servers/Steam/steamcmd_linux.tar.gz &&
echo "Steam Installation and Update Completed"
It's not doing anything if im running it from WebPage, but it works when im trying to run it under the SSH user (same as apache user with permissions to write to this folder)
So my question, what am i doing wrong?
UPDATE:
My php code to run script is
if($path[2] === 'steaminstall')
{
$InstallSteam = exec('/var/www/aftersoft/data/Servers/Steam/SteamInstall', $Output, $Error);
if($Error === 0)
{
$Status = $DB->SteamStatus($_SESSION['username']);
if($Status)
{
header('Location: /cp/'.$_SESSION['username'].'/services/');
}
else
{
$Smarty->assign('InstallStatus', 'Database Error Occured');
$Smarty->display('steaminstall.tpl');
}
}
if($Error === 2)
{
$Smarty->assign('InstallStatus', $Output);
$Smarty->display('steaminstall.tpl');
}
}
And this is my database query function
public function SteamStatus($Username)
{
$Status = '1';
$Statement = $this->DBConnection->prepare("UPDATE account set steaminstalled = ? where username = ?");
$Statement->bindParam(1, $Status);
$Statement->bindParam(2, $Username);
$IStatus = $Statement->execute();
if($IStatus)
{
return true;
}
else
{
return false;
}
}
Problem solved, you just need to specify direct path to your script (as if you dont, the script itself dont know where to download and where to extract)
So the final bash script (PHP part is perfectly fine):
#!/bin/bash
wget -P /var/www/aftersoft/data/Servers/Steam/ http://media.steampowered.com/client/steamcmd_linux.tar.gz &&
tar xvfz /var/www/aftersoft/data/Servers/Steam/steamcmd_linux.tar.gz -C /var/www/aftersoft/data/Servers/Steam/ &&
sh /var/www/aftersoft/data/Servers/Steam/steamcmd.sh +login anonymous +quit &&
rm /var/www/aftersoft/data/Servers/Steam/steamcmd_linux.tar.gz &&
echo "Steam Installation and Update Completed"

Categories