Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Linux terminal command in PHP

Tags:

linux

php

I have a server running on Linux that execute commands to 12 nodes (12 computers with Linux running in them). I recently downloaded PHP on the server to create web pages that can execute commands by opening a specific PHP file.

I used exec(), passthru(), shell_​exec(), and system(). system() is the only one that returns a part of my code. I would like PHP to act like open termainal command in linux and I cannot figure out how to do it!

Here is an example of what is happening now (Linux directly vs PHP): When using linux open terminal command directly:

user@wizard:/home/hyperwall/Desktop> /usr/local/bin/chbg -mt

I get an output:

The following settings will be used:
  option = mtsu   COLOR =    IMAGE = imagehereyouknow!
  NODES  = LOCAL
and additional code to send it to 12 nodes.

Now with PHP:

switch($_REQUEST['do'])
{ case 'test':
    echo system('/usr/local/bin/chbg -mt');
    break;
}

Output:

The following settings will be used:
  option = mtsu   COLOR =    IMAGE = imagehereyouknow!
  NODES  = LOCAL

And stops! Anyone has an explanation of what is happening? And how to fix it? Only system displays part of the code the other functions display nothing!

like image 366
syrkull Avatar asked Feb 11 '15 23:02

syrkull


2 Answers

My First thought is it can be something about std and output error. Some softwares dump some informations on std out and some in std error. When you are not redirecting std error to std out, most of the system calls only returns the stdout part. It sounds thats why you see the whole output in terminal and can't in the system calls. So try with

 /usr/local/bin/chbg -mt 2>&1  

Edit: Also for a temporary work through, you can try some other things. For example redirect the output to file next to the script and read its contents after executing the command, This way you can use the exec:

exec("usr/local/bin/chbg -mt 2>&1 > chbg_out");
//Then start reading chbg_out and see is it work

Edit2 Also it does not make sense why others not working for you. For example this piece of code written in c, dumps a string in stderr and there is other in stdout.

#include <stdio.h>
#include<stdlib.h>
int main()
{

    fputs("\nerr\nrro\nrrr\n",stderr);
    fputs("\nou\nuu\nuttt\n",stdout);
    return 0;
}

and this php script, tries to run that via exec:

<?php

exec("/tmp/ctest",&$result);

foreach ( $result as $v )
{
echo $v;
}
#output ouuuuttt
?>

See it still dumps out the stdout. But it did not receive the stderr. Now consider this:

<?php

exec("/tmp/ctest 2>&1",&$result);

foreach ( $result as $v )
{
    echo $v;
}
//output: errrrorrrouuuuttt
?>

See, this time we got the whole outputs.

This time the system:

<?php
     echo system("/tmp/ctest 2>&1");
     //output: err rro rrr ou uu uttt uttt
?>

and so on ...

like image 132
madz Avatar answered Oct 17 '22 04:10

madz


Maybe your chbg -mt writes additional code to stderr instead of stdout? Try to execute your script inside php like this:

/usr/local/bin/chbg -mt 2>&1
like image 2
renskiy Avatar answered Oct 17 '22 06:10

renskiy