Linux terminal open command in PHP

I have a server running on Linux that runs commands up to 12 nodes (12 Linux computers work on them). I recently uploaded PHP to a 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 part of my code. I would like PHP to work as an open terminology command in linux, and I cannot figure out how to do this!

Here is an example of what is happening now (Linux directly against PHP): When using the linux command line directly:

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

I get the 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; } 

Exit:

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

And it stops! Does anyone explain what is happening? And how to fix it? Only the system displays part of the code, other functions do not display anything!

+5
source share
8 answers

My first thought is that it might be something about std and an output error. Some tools unload some data about the STD, and some in the std error. If you do not redirect the std error to std-out, most system calls return only a portion of stdout. It looks like you see all the output in the terminal and cannot in system calls. Therefore try

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

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

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

Edit2 It also doesn't make sense why others don't work for you. For example, this piece of code written in c produces a string in stderr, and in stdout another.

 #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, trying to run this through exec:

 <?php exec("/tmp/ctest",&$result); foreach ( $result as $v ) { echo $v; } #output ouuuuttt ?> 

Look, it unloads the original string anyway. But he did not get stderr. Now consider the following:

 <?php exec("/tmp/ctest 2>&1",&$result); foreach ( $result as $v ) { echo $v; } //output: errrrorrrouuuuttt ?> 

Look, this time we got the whole results.

This time the system:

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

etc.

+4
source

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

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

Other answers are useful for general advice. But in this particular case, it seems that you are trying to change the background on the desktop. This requires many special considerations due to the "user context":

  • Firstly, your web server probably works as a different user and, therefore, does not have permission to change the desktop.

  • Secondly, the program may require some environmental variables from your user context. For example, for X programs, you need the DISPLAY variable, ssh-agent needs SSH_AGENT_PID and SSH_AUTH_SOCK , etc. I don't know much about changing the background, but I suppose it's D-Bus related, which probably requires things like DBUS_SESSION_BUS_ADDRESS , KONSOLE_DBUS_SERVICE , KONSOLE_DBUS_SESSION and KONSOLE_DBUS_WINDOW . There may be many others. Please note that some of these vars change every time you log in, so you cannot hardcode them on the PHP side.

For testing, it may be easier to start your own web server directly from a user session. (i.e. do not use the system one, it should work like you. You will need to run it on an alternative port, for example 8080). The web server that you start manually will have the entire β€œcontext” that it needs. I mentioned websocketd because it just came out and looks neat.

For production, you may need to run a daemon from time to time in a user context, and the web server must talk to that daemon to β€œget stuff” in a user context.

+2
source

The PHP system returns only the last line of execution:

Return value: returns the last line of the command output file on success and FALSE on failure.

Most likely, you will want to use either exec or passthru . exec has an optional parameter for outputting output to an array. You can detonate the output and use it to echo.

 switch($_REQUEST['do']) { case 'test': exec('/usr/local/bin/chbg -mt', $output); echo implode('\n', $output); // Could use <br /> if HTML output is desired break; } 
+2
source

I think that the result of execution may vary between users. First try running a PHP script directly in your terminal php yourScript.php
If it works as expected, go to your Apache service and update it to run with your credentials

+1
source

You are trying to change the background for currently registered users ... While they are using the desktop. For example, when I print this message. I minimize my browser and "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooh. I hope this is for something important, as it turns red when the reactor or overheating.

Anyway, to my answer:

Instead of trying to remotely connect and run items as separate users. Configure each user to run a bash script (in their own account in their shell) on a repeating timer. Say every 10 minutes. Select the SAME .. file from the network location /somenetworkshare/backgrounds/images/current.png

Then you can update ALL nodes (from 1 to a million) by simply changing the image itself in / somenetworkshare / backgrounds / images / current.png

+1
source

I wrote something a long time ago that does just that: you can run the command interpreter (/ bin / sh), send its commands, read the answers, send more commands, etc. It uses proc_open() to open a child process and talk to it.

It is located at http://github.com/andrasq/quicklib , Quick / Proc / Process.php

Using this will look something like this (easier if you have a flexible autoloader, I also wrote one of them in Quicklib):

 include 'lib/Quick/Proc/Exception.php'; include 'lib/Quick/Proc/Exists.php'; include 'lib/Quick/Proc/Process.php'; $proc = new Quick_Proc_Process("/bin/sh"); $proc->putInput("pwd\n"); $lines = $proc->getOutputLines($nlines = 10, $timeoutSec = 0.2); echo $lines[0]; $proc->putInput("date\n"); $lines = $proc->getOutputLines(1, 0.2); echo $lines[0]; 

Outputs

 /home/andras/quicklib Sat Feb 21 01:50:39 EST 2015 

The unit of communication between php and the process is the lines with the final extension of the line. All commands must end with a newline, and all responses are retrieved in units of lines. Do not forget about the new characters, they are difficult to identify subsequently.

+1
source

I am working on a project that uses terminal A on machine A to output to terminal B on machine B using Linux. I have not seen this mentioned, but maybe you can use a redirect, something like this on your web server:

 switch($_REQUEST['do']) { case 'test': #process ID on the target (12345, 12346 etc) echo system('/usr/local/bin/chbg -mt > /proc/<processID>/fd/1'); #OR #device file on the target (pts/0,tty0, etc) echo system('/usr/local/bin/chbg -mt > /dev/<TTY-TYPE>/<TTYNUM>'); break; } 

Definitely, permissions must be set correctly for this. The "mesg y" command in the terminal can also help ... Hope this helps.

0
source

Source: https://habr.com/ru/post/1213184/


All Articles