PHP implementation of the Linux system commands commonly used function instructions _php tips

Source: Internet
Author: User

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, which executes the instruction and outputs the result. If the Return_var parameter exists, the state after executing the command is filled in the Return_var. Also noteworthy is that if you need to deal with user input data, but also to prevent users to cheat the system, you can use Escapeshellcmd (). If PHP is executed in a modular form, this function automatically updates the output buffer registers of the WEB server after each row output. You can use PassThru () If you need a complete return string and you do not want to go through unnecessary other intermediate output interfaces.


Instance code:

Copy Code code as follows:

< PHP
$last _line = System (' ls ', $retval);
Echo ' last line of the output: '. $last _line;
Echo ' ?>

exec function
Description: Perform external programs.
Syntax: string exec (String command, string [array], int [return_var]);
return value: String

Detailed Introduction:
This function executes an external program or an external instruction that enters the command. Its return string is only the last line returned after the execution of the external program, and you can use the PassThru () function if you need a full return string.

If the parameter array exists, the command adds the array to the parameter and, if the array is not processed, call unset () before executing exec (). If the Return_var and array two parameters exist, the state after the command execution is filled in the Return_var.

It is worth noting that if you need to deal with user input information, but also to prevent users from playing tricks to crack the system, you can use Escapeshellcmd ().

Instance code:
Copy Code code as follows:

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

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

Detailed Introduction:
This function executes an instruction file that is processed by pipe. 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 a value of False if an error occurs in the open file.

Instance code:
Copy Code code as follows:

<?
$fp = Popen ("/bin/ls", "R");
?>


PHP monitors linux server load

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

Because of the Web server and the way PHP is implemented, it's hard to use PHP in a real-world environment to invoke programs that require root in Linux to execute, and I find another way to circumvent this limitation on the web. First write a C program to invoke system commands, and then use PHP to execute this C program.

C Program

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

#include <stdio.h>
#include <stdlib.h>
#include <systypes.h>
#include <unistd.h>

int main ()
{
uid_t uid, euid;

Note To get 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 commands
System ("/usr/bin/w");
Return0;
}

Compiling the file Gcc-o W-wall w.c, at which point the program W is generated in the current directory. The owner chmod U+s./w that changed the program.
PHP execution

The contents of the file are as follows, placed in the Web directory, the access will output the current server load situation.
Copy Code code as follows:

<?php
/*
More & Original PHP framwork
Copyright (c) 2007-2008 Ismole Inc.

$Id: servermonitor.php 408 2008-12-02 08:07:40z Kimi $
*/

The validation process for note key
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 Linux system commands we want to perform, SVN update, server monitoring, backup, recovery, day-to-day maintenance, and so on.

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.