Instructions for using common functions in PHP to execute Linux system commands_PHP tutorial

WBOY
Release: 2016-07-21 15:38:34
Original
940 people have browsed it

system function
Description: 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 function system() in C language , used to execute instructions and output results. If the return_var parameter exists, the status after executing the command will be filled in return_var. It is also worth noting that if you need to process the data entered by the user, but want to prevent the user from playing tricks to crack the system, you can use EscapeShellCmd(). If PHP is executed in a modular fashion, this function will automatically update the output buffer buffer of the web server after each line of output. If you need to return a complete string and don't want to go through unnecessary other intermediate output interfaces, you can use PassThru().


Example code:

Copy code The code is as follows:

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

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

Detailed introduction:
This function Execute the external program or external instruction entered command. Its return string is only the last line returned after the external program is executed; if you need a complete return string, you can use the PassThru() function.

If the parameter array exists, the command will add the array to the parameter for execution. If you do not want the array to be processed, you can call unset() before executing exec(). If both return_var and array parameters exist, the status after executing the command will be filled in return_var.

It is worth noting that if you need to process the data entered by the user and prevent the user from playing tricks to crack the system, you can use EscapeShellCmd().

Example code:
Copy code The code is as follows:

< ?php
echo exec ('whoami');
?>

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

Detailed introduction:
This function executes the instruction to open the file, and the file is processed by pipeline document. Files opened with this function can only be one-way (read only or write only), and must be closed with pclose(). For file operations, fgets(), fgetss(), and fputs() can be used. If an error occurs while opening the file, a false value is returned.

Example code:
Copy code The code is as follows:

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


PHP monitor linux server load

In actual project applications, due to the reality of various conditions, using PHP to implement server load monitoring will be a more flexible way.

Due to the limitations of the implementation of Web Server and PHP, it is difficult for us to use PHP to call some programs in Linux that require root permissions to execute in the real environment. For this, I found another way from the Internet to get around this limitation. First, write a C program to transfer the system command, and then use PHP to execute the C program.

c program

First write a c file, such as /usr/local/ismole/w.c
Copy code Code As follows:

#include
#include
#include
#include

int main()
{
uid_t uid ,euid;

//note Get the current uid
uid = getuid();
//note Get the current euid
euid = geteuid();

//note Exchange these two ids
if(setreuid(euid, uid))
perror("setreuid");

//note The execution will execute the linux system command
system("/usr/bin/w");
return0;
} 

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

The content of the file is as follows. If placed in the web directory, the current server load will be output when accessed.
Copy code The code is as follows:

/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.

$Id: serverMonitor.php 408 2008 -12-02 08:07:40Z kimi $
*/

//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;

According to the above example, we can use PHP to do it Any Linux system commands we want to perform, SVN updates, server monitoring, backup, recovery, daily maintenance, etc.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321692.htmlTechArticlesystem function description: Execute external programs and display output data. Syntax: string system(string command, int [return_var]); Return value: string Detailed introduction: This function is like C...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!