PHP Shell脚本与系统命令调用PHP可以调用系统命令执行外部程序。但要注意安全问题。今天说说PHP中执行系统命令的方法。执行系统命令的几种方式。php// exec - 返回最后一行输出$lastLine exec(ls -la, $output, $code);echo 最后一行: $lastLine\n;echo 退出码: $code\n;// shell_exec - 返回全部输出$output shell_exec(ls -la);echo $output;// system - 直接输出结果system(ls -la, $code);// passthru - 输出原始结果passthru(ls -la, $code);?安全地执行命令。phpfunction safeExec(string $command, array $args []): array{$escapedArgs array_map(escapeshellarg, $args);$fullCommand $command . . implode( , $escapedArgs);$output [];$code 0;exec($fullCommand, $output, $code);return [output $output, code $code];}$result safeExec(ls, [-la, /tmp]);print_r($result);// escapeshellcmd - 转义整个命令// escapeshellarg - 转义单个参数?管道操作。php$output shell_exec(ps aux | grep php | wc -l);echo PHP进程数: . trim($output) . \n;$output shell_exec(cat /proc/cpuinfo | grep processor | wc -l);echo CPU核心数: . trim($output) . \n;?命令执行的超时控制。phpfunction execWithTimeout(string $command, int $timeout 5): array{$command timeout $timeout $command 21;$output [];$code 0;exec($command, $output, $code);if ($code 124) {throw new RuntimeException(命令执行超时);}return [output $output, code $code];}try {$result execWithTimeout(sleep 10, 2);} catch (RuntimeException $e) {echo 超时: {$e-getMessage()}\n;}?禁用危险函数。ini; php.inidisable_functions exec,passthru,shell_exec,system,popen,proc_openCLI模式下的实用脚本。php#!/usr/bin/env phpif (php_sapi_name() ! cli) die(CLI only\n);$options getopt(, [name:, help]);if (isset($options[help])) {echo 用法: php script.php --name名称\n;exit(0);}$name $options[name] ?? World;echo Hello, $name!\n;?PHP调用系统命令很方便但要注意安全。escapeshellarg转义参数防止注入。超时控制防止命令挂起。生产环境建议禁用exec等危险函数。CLI模式下PHP可以编写强大的运维脚本
PHPShell脚本与系统命令调用
发布时间:2026/6/6 3:55:08
PHP Shell脚本与系统命令调用PHP可以调用系统命令执行外部程序。但要注意安全问题。今天说说PHP中执行系统命令的方法。执行系统命令的几种方式。php// exec - 返回最后一行输出$lastLine exec(ls -la, $output, $code);echo 最后一行: $lastLine\n;echo 退出码: $code\n;// shell_exec - 返回全部输出$output shell_exec(ls -la);echo $output;// system - 直接输出结果system(ls -la, $code);// passthru - 输出原始结果passthru(ls -la, $code);?安全地执行命令。phpfunction safeExec(string $command, array $args []): array{$escapedArgs array_map(escapeshellarg, $args);$fullCommand $command . . implode( , $escapedArgs);$output [];$code 0;exec($fullCommand, $output, $code);return [output $output, code $code];}$result safeExec(ls, [-la, /tmp]);print_r($result);// escapeshellcmd - 转义整个命令// escapeshellarg - 转义单个参数?管道操作。php$output shell_exec(ps aux | grep php | wc -l);echo PHP进程数: . trim($output) . \n;$output shell_exec(cat /proc/cpuinfo | grep processor | wc -l);echo CPU核心数: . trim($output) . \n;?命令执行的超时控制。phpfunction execWithTimeout(string $command, int $timeout 5): array{$command timeout $timeout $command 21;$output [];$code 0;exec($command, $output, $code);if ($code 124) {throw new RuntimeException(命令执行超时);}return [output $output, code $code];}try {$result execWithTimeout(sleep 10, 2);} catch (RuntimeException $e) {echo 超时: {$e-getMessage()}\n;}?禁用危险函数。ini; php.inidisable_functions exec,passthru,shell_exec,system,popen,proc_openCLI模式下的实用脚本。php#!/usr/bin/env phpif (php_sapi_name() ! cli) die(CLI only\n);$options getopt(, [name:, help]);if (isset($options[help])) {echo 用法: php script.php --name名称\n;exit(0);}$name $options[name] ?? World;echo Hello, $name!\n;?PHP调用系统命令很方便但要注意安全。escapeshellarg转义参数防止注入。超时控制防止命令挂起。生产环境建议禁用exec等危险函数。CLI模式下PHP可以编写强大的运维脚本