Common functions for PHP to execute Linux system commands

  • 2020-03-31 20:34:23
  • OfStack

The system function
Execute external program and display output data.
Syntax: string system(string command, int [return_var]);
Return value: string

Detailed introduction:
This function is like the system() function in C, which is used to execute instructions and output results. If the return_var parameter exists, the state after command execution is filled into return_var. It is also worth noting that EscapeShellCmd() can be used if you want to process user input data and prevent users from cheating to break the system. If PHP is executed as a module, this function automatically updates the output buffer staging area of the Web server after each line of output. You can use PassThru() if you want a complete return string and don't want to go through other intermediate output interfaces that are not necessary.


Example code:

 
< ?php 
$last_line = system('ls', $retval); 
echo 'Last line of the output: ' . $last_line; 
echo '<hr />Return value: ' . $retval; 
?> 

The exec function
Execute external programs.
Syntax: string exec(string command, string [array], int [return_var]);
Return value: string

Detailed introduction:
This function executes an external program or instruction that is input to the command. Its return string is just the last line returned by an external program after execution; For a complete return string, use the function PassThru().

If the array exists, the command adds the array to the argument and, if it does not want the array to be processed, it can call unset() before exec() is executed. If both the return_var and array arguments exist, the state after command execution is filled into return_var.

It is worth noting that EscapeShellCmd() can be used if you need to process user input data and prevent users from cheating to break the system.

Example code:
 
< ?php 
echo exec('whoami'); 
?> 

Popen function
Open the file.
Syntax: int popen(string command, string mode);
Return value: integer

Detailed introduction:
This function performs an instruction to open the file, which is piped. Files opened with this function can only be one-way (read only or write only) and must be closed with pclose(). You can use fgets(), fgetss(), and fputs() on file operations. Returns false in case of an error in opening.

Example code:
 
< ? 
$fp = popen("/bin/ls","r" ); 
?> 


PHP monitors Linux server load

In the actual application of the project, because of the reality of various conditions, using PHP to achieve server load monitoring will be a more flexible way.

Due to the limitations of Web Server and PHP implementations, it is difficult to use PHP in the real world to invoke programs that require root permission to execute in Linux. Therefore, I found another way to get around this limitation from the Internet. First, write a c program in transit to call the system command, and then use PHP to execute the c program.

C program

Start by writing a c file, such as /usr/local/ismole/w.c
 
#include<stdio.h> 
#include<stdlib.h> 
#include<systypes.h> 
#include<unistd.h> 

int main() 
{ 
uid_t uid ,euid; 

//Note gets the current uid
uid = getuid(); 
//Note gets the current euid
euid = geteuid(); 

//Note exchanges these two ids
if(setreuid(euid, uid)) 
perror("setreuid"); 

//Note execution will execute the Linux system command
system("/usr/bin/w"); 
return0; 
}   

Compile the file gcc-o w-wall w.c and the program w will be generated in the current directory. Change the owner of this program chmod u+ s. /w.
PHP execution

The file contents are as follows, placed in the web directory, access will output the current server load.
 
<?php 
 

//Note key verification process
if($key != $authkey) { 
// exit('key error); 
} 

$last_line = exec('/usr/local/ismole/w', $retval); 

$returnArray = explode("load average: ", $retval[0]); 
$returnString = $returnArray[1]; 

echo $returnString;  

Following the example above, we can use PHP to do any Linux system commands we want, SVN updates, server monitoring, backup, restore, routine maintenance, and so on.


Related articles: