PHP common functions for executing Linux system commands _php tutorial

Source: Internet
Author: User
Tags svn update root access
system Functions
Description: Executes external program and displays output data.
Syntax: string system (String command, int [Return_var]);
return value: String

Detailed Description:
This function is like the function system () in C, which executes the instruction and outputs the result. If the Return_var parameter exists, the state after the command is executed is populated with Return_var. It is also worth noting that if you need to deal with user input data, but also to prevent users to hack the system, you can use Escapeshellcmd (). If PHP is executed in a modular style, this function automatically updates the output buffer staging area of the WEB server after each line output. If you need a complete return string and do not want to go through an unnecessary other intermediate output interface, you can use PassThru ().


Instance code:
Copy CodeThe 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: Executes an external program.
Syntax: string exec (String command, string [array], int [return_var]);
return value: String

Detailed Description:
This function executes an external program or external instruction that enters the command. Its return string is just the last line returned after the external program executes, and the PassThru () function can be used if a complete return string is required.

If the parameter array is present, command will add the array to the parameter, and if you do not want the array to be processed, you can call unset () before executing exec (). If Return_var and array two parameters are present, the state after the command is executed is populated with Return_var.

It is worth noting that if you need to deal with user input data, but also to prevent users to hack the system, you can use Escapeshellcmd ().

Instance code:
Copy CodeThe code is as follows:
< PHP
echo exec (' whoami ');
?>

Popen function
Description: Opens the file.
Syntax: int popen (String command, string mode);
return value: Integer

Detailed Description:
This function executes the instruction file, and the file is processed in a pipe manner. 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 the False value if an error occurs on the file.

Instance code:
Copy CodeThe code is as follows:
<?
$fp = Popen ("/bin/ls", "R");
?>


PHP monitors linux server load

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

Because of the limitations of Web server and the way PHP is implemented, it is difficult for us to use PHP in a real world to invoke programs that require root access in Linux, and I find another way to circumvent this restriction on the web. First write a C program to call the System command, and then use PHP to execute the C program.

C Program

First write a C file, like/usr/local/ismole/w.c.
Copy CodeThe code is as follows:
#include
#include
#include
#include

int main ()
{
uid_t uid, euid;

Note To obtain the current UID
UID = Getuid ();
Note Get current Euid
Euid = Geteuid ();

Note Exchange these two IDs
if (Setreuid (Euid, UID))
Perror ("Setreuid");

Note Execution will execute Linux system command
System ("/usr/bin/w");
Return0;
}

Compile the file Gcc-o w-wall w.c, which will generate the program W in the current directory. The master chmod u+s./w that changed this program.
PHP execution

The contents of the file are as follows, and in the Web directory, Access will output the current server load situation.
Copy CodeThe 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 any of the Linux system commands we want to execute, SVN update, server monitoring, backup, recovery, routine maintenance and so on.

http://www.bkjia.com/PHPjc/321692.html www.bkjia.com true http://www.bkjia.com/PHPjc/321692.html techarticle description of the system function: executes the external program and displays the output data. Syntax: string system (String command, int [Return_var]); Return value: String Details: This function is like C ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.