Home Backend Development PHP Tutorial PHP执行linux系统命令的常用函数使用说明_PHP

PHP执行linux系统命令的常用函数使用说明_PHP

Jun 01, 2016 pm 12:20 PM
linux php 系统命令

system函数
说明:执行外部程序并显示输出资料。
语法:string system(string command, int [return_var]);
返回值: 字符串

详细介绍:
本函数就像是 C 语中的函数 system(),用来执行指令,并输出结果。若是 return_var 参数存在,则执行 command 之后的状态会填入 return_var 中。同样值得注意的是若需要处理用户输入的资料,而又要防止用户耍花招破解系统,则可以使用 EscapeShellCmd()。若 PHP 以模块式的执行,本函数会在每一行输出后自动更新 Web 服务器的输出缓冲暂存区。若需要完整的返回字符串,且不想经过不必要的其它中间的输出界面,可以使用 PassThru()。


实例代码:
复制代码 代码如下:
$last_line = system('ls', $retval);
echo 'Last line of the output: ' . $last_line;
echo '


Return value: ' . $retval;
?>

exec函数
说明:执行外部程序。
语法:string exec(string command, string [array], int [return_var]);
返回值: 字符串

详细介绍:
本函数执行输入 command 的外部程序或外部指令。它的返回字符串只是外部程序执行后返回的最后一行;若需要完整的返回字符串,可以使用 PassThru() 这个函数。

要是参数 array 存在,command 会将 array 加到参数中执行,若不欲 array 被处理,可以在执行 exec() 之前呼叫 unset()。若是 return_var 跟 array 二个参数都存在,则执行 command 之后的状态会填入 return_var 中。

值得注意的是若需要处理使用者输入的资料,而又要防止使用者耍花招破解系统,则可以使用 EscapeShellCmd()。

实例代码:
复制代码 代码如下:
echo exec('whoami');
?>

popen函数
说明:打开文件。
语法:int popen(string command, string mode);
返回值: 整数

详细介绍:
本函数执行指令开档,而该文件是用管道方式处理的文件。用本函数打开的文件只能是单向的 (只能读或只能写),而且一定要用 pclose() 关闭。在文件操作上可使用 fgets()、fgetss() 与 fputs()。若是开档发生错误,返回 false 值。

实例代码:
复制代码 代码如下:
$fp = popen("/bin/ls","r" );
?>


PHP监控linux服务器负载

  在实际项目的应用中,我们由于各种条件的现实,利用PHP来实现服务器负载监控将是一种更为灵活的方式。

  由于Web Server以及PHP的实现方式所限,我们在现实环境中很难利用PHP去调用一些Linux中需要root权限才能执行的程序,对此,我从网上找到另外一种方式来绕开这个限制。首先先写个c程序中转调用系统命令,然后用PHP去执行此c程序。

c程序

首先写个c文件,比如/usr/local/ismole/w.c
复制代码 代码如下:
#include
#include
#include
#include

int main()
{
uid_t uid ,euid;

//note 获得当前的uid
uid = getuid();
//note 获得当前euid
euid = geteuid();

//note 交换这两个id
if(setreuid(euid, uid))
perror("setreuid");

//note 执行将要执行linux系统命令
system("/usr/bin/w");
return0;
}  

编译该文件gcc -o w -Wall w.c,这时会在当前目录下生成程序w。改变此程序的属主chmod u+s ./w。
PHP执行

文件内容如下,放在web目录下,访问就会输出当前的服务器负载情况。
复制代码 代码如下:
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.

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

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

 按照上面的实例,我们可以用PHP来做任何我们想执行的Linux系统命令,SVN更新,服务器监控,备份,恢复,日常维护等等。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1237
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Docker on Linux: Containerization for Linux Systems Docker on Linux: Containerization for Linux Systems Apr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

See all articles