Open Linux terminal command in PHP

syrkull :

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!

madz :

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 ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linux, Python open terminal run global python command

From Java

Terminal command to open Safari

From Dev

How to run a linux terminal command with PHP non-blocking?

From Dev

Open Raspberry Pi Terminal via Terminal Command?

From Dev

Run a scheduled command on open terminal

From Dev

Command `open` not working on Ubuntu Terminal

From Dev

Command for open terminal to a specified path

From Dev

Mac Terminal Find and Open command

From Dev

open terminal run command python

From Dev

open a man page in linux terminal

From Dev

Linux - Open terminal for input/output

From Dev

script to open terminal, run command and keep terminal open

From Dev

Linux terminal command execution with java

From Dev

Open new gnome-terminal and run command

From Dev

Nautilus open terminal in remote edit command

From Dev

Open a terminal running a command if a systemd service fails

From Dev

Terminal command to open new Safari window

From Dev

Execute a command every time Terminal is open

From Dev

Command to open new tab in the current terminal

From Dev

-bash: 400:: command not found when open terminal

From Dev

terminal command to open new chromium tab

From Dev

Execute command as if in another open terminal window

From Dev

Code command in terminal doesn't open in intellij

From Dev

Open Windows Terminal PREVIEW from command line

From Dev

Command to open new terminal window from the current terminal?

From Dev

Opening a terminal, running a command, and leaving an interactive terminal open

From Dev

open a terminal in vim and execute a command inside this terminal on windows

From Dev

MacOS 'open'-command for GNU/Linux

From Dev

How to open process again in linux terminal?

Related Related

HotTag

Archive