PHP exec - 检查是否启用或禁用

发布于 2024-08-30 19:44:08 字数 55 浏览 3 评论 0原文

有没有办法检查 php 脚本是否在服务器上启用或禁用 exec()

Is there a way to check in a php script if exec() is enabled or disabled on a server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

亚希 2024-09-06 19:44:08

这将检查该功能是否确实有效(权限、权利等):

if(@exec('echo EXEC') == 'EXEC'){
    echo 'exec works';
}

This will check if the function actually works (permissions, rights, etc):

if(@exec('echo EXEC') == 'EXEC'){
    echo 'exec works';
}
思念满溢 2024-09-06 19:44:08
if(function_exists('exec')) {
    echo "exec is enabled";
}
if(function_exists('exec')) {
    echo "exec is enabled";
}
带上头具痛哭 2024-09-06 19:44:08

ini_get('disable_functions')

您真正想要做的是使用 ini_get('disable_functions') 来了解它是否可供您使用:

<?php
function exec_enabled() {
    $disabled = explode(',', ini_get('disable_functions'));
    return !in_array('exec', $disabled);
}
?>

在 stackoverflow 上回答如下:< a href="https://stackoverflow.com/questions/3938120/check-if-exec-is-disabled">检查“exec”是否被禁用,这实际上似乎来自 PHP 手册页: http://php.net/manual/en/function.exec.php#97187

Path

如果上面返回 true(您可以使用 exec()),但 PHP 仍然无法触发脚本,则很有可能您的脚本存在路径问题,请测试这样做:

print exec('which bash');

然后尝试

print exec('which ogr2ogr');

ini_get('disable_functions')

What you actually want to do is use ini_get('disable_functions') to find out if it is available to you:

<?php
function exec_enabled() {
    $disabled = explode(',', ini_get('disable_functions'));
    return !in_array('exec', $disabled);
}
?>

Answered on stackoverflow here: Check if "exec" is disabled, Which actually seems to come from the PHP Man page: http://php.net/manual/en/function.exec.php#97187

Path

If the above returns true (you can use exec()), but PHP can still not trigger the script there is a good chance that you have a path issue for that script, test this by doing:

print exec('which bash');

and then try

print exec('which ogr2ogr');
软糖 2024-09-06 19:44:08

这将在尝试运行之前检查 exec 是否可用并已启用。如果运行 exec() 并且该函数不存在或被禁用,则会生成警告。根据可能呈现给浏览器的服务器设置,并且几乎总是将一行写入日志文件=性能影响。

// Exec function exists.
// Exec is not disabled.
// Safe Mode is not on.
$exec_enabled =
   function_exists('exec') &&
   !in_array('exec', array_map('trim', explode(', ', ini_get('disable_functions')))) &&
   strtolower(ini_get('safe_mode')) != 1
;


if($exec_enabled) { exec('blah'); }

This will check that exec is available and enabled BEFORE trying to run it. If you run exec() and the function does not exist or is disabled a warning will be generated. Depending on the server settings that may render to the browser and will almost-always write a line to a log file = performance hit.

// Exec function exists.
// Exec is not disabled.
// Safe Mode is not on.
$exec_enabled =
   function_exists('exec') &&
   !in_array('exec', array_map('trim', explode(', ', ini_get('disable_functions')))) &&
   strtolower(ini_get('safe_mode')) != 1
;


if($exec_enabled) { exec('blah'); }
青春如此纠结 2024-09-06 19:44:08

找到可用的 exec 函数有点棘手,除非检查所有可能性

1.function_exist('exec')

2.扫描 ini_get('disabled_functions)

3.检查 safe_mode 已启用

function is_shell_exec_available() {
    if (in_array(strtolower(ini_get('safe_mode')), array('on', '1'), true) || (!function_exists('exec'))) {
        return false;
    }
    $disabled_functions = explode(',', ini_get('disable_functions'));
    $exec_enabled = !in_array('exec', $disabled_functions);
    return ($exec_enabled) ? true : false;
}

除非 ini_get 函数未禁用,否则此函数绝不会引发警告。

It's a bit tricky to find exec function available until unless checking all possibilities

1.function_exist('exec')

2.Scanning through ini_get('disabled_functions)

3.Checking safe_mode enabled

function is_shell_exec_available() {
    if (in_array(strtolower(ini_get('safe_mode')), array('on', '1'), true) || (!function_exists('exec'))) {
        return false;
    }
    $disabled_functions = explode(',', ini_get('disable_functions'));
    $exec_enabled = !in_array('exec', $disabled_functions);
    return ($exec_enabled) ? true : false;
}

This function never throws warnings unless ini_get function not disabled.

我也只是我 2024-09-06 19:44:08

我假设您正在 Linux 服务器上运行它。

您可以通过运行以下 php 脚本来测试 exec 函数:

exec("whoami", $ret);

echo $ret[0];

这将返回命令 whoami。

如果出错,则是因为exec函数无法运行。

I am assuming that you are running this on a linux server.

You could test the exec function by running the following php script:

exec("whoami", $ret);

echo $ret[0];

This will return the command whoami.

If it errors out, it is because the exec function could not run.

ま柒月 2024-09-06 19:44:08

例子:

if(strpos(ini_get('disable_functions'),'ini_set')===false) 
    @ini_set('display_errors',0);

Example:

if(strpos(ini_get('disable_functions'),'ini_set')===false) 
    @ini_set('display_errors',0);
苦行僧 2024-09-06 19:44:08

这是我编写的一些丑陋的代码,用于检测某个功能是否启用。

function is_enabled($f)
{
    if($f=='ini_get')return@ini_get('a')===false;
    return(($l=@ini_get('disable_functions'))===null||!is_callable($f)||!function_exists($f)||!in_array($f,array_map('trim',explode(',',$l)));
}

//Usage example:
print_r(is_enabled('str_split'));//true or null if ini_get() is disabled

This is some ugly code I made to detect if a function is enabled or not.

function is_enabled($f)
{
    if($f=='ini_get')return@ini_get('a')===false;
    return(($l=@ini_get('disable_functions'))===null||!is_callable($f)||!function_exists($f)||!in_array($f,array_map('trim',explode(',',$l)));
}

//Usage example:
print_r(is_enabled('str_split'));//true or null if ini_get() is disabled
浪漫人生路 2024-09-06 19:44:08

(基于其他回复)
要检查 exec 是否存在并且服务是否正在运行:

function isRunning($serviceName)
{
    return exec('pgrep '.$serviceName);
}

if (@exec('echo EXEC') == 'EXEC') {
    $services = [
        'mysql',
        'nginx',
        'redis',
        'supervisord',
    ];

    foreach ($services as $service) {
        if (isRunning($service)) {
            echo $service.' service is running.<br>';
        } else {
            echo $service.' service is down.<br>';
        }
    }
}

// mysql service is running.
// nginx service is running.
// redis service is running.
// supervisord service is down.

(Based on other responses)
To check if exec exists and services are running:

function isRunning($serviceName)
{
    return exec('pgrep '.$serviceName);
}

if (@exec('echo EXEC') == 'EXEC') {
    $services = [
        'mysql',
        'nginx',
        'redis',
        'supervisord',
    ];

    foreach ($services as $service) {
        if (isRunning($service)) {
            echo $service.' service is running.<br>';
        } else {
            echo $service.' service is down.<br>';
        }
    }
}

// mysql service is running.
// nginx service is running.
// redis service is running.
// supervisord service is down.
杯别 2024-09-06 19:44:08

我会用这个:

if (in_array('exec', preg_split('/\s*,\s*/', ini_get('disable_functions')) {
  echo "exec is disabled";
}

I would use this:

if (in_array('exec', preg_split('/\s*,\s*/', ini_get('disable_functions')) {
  echo "exec is disabled";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文