在PHP中获取Linux发行版名称

问题描述 投票:0回答:4

PHP 有没有办法找出远程服务器的 Linux 发行版名称?

$_SERVER['HTTP_USER_AGENT']
中提取的只是客户端机器的操作系统名称。这不是我想要的。我尝试过php_uname()

echo 'Operating System: '.php_uname('s').'<br>'; // echo PHP_OS;
echo 'Release Name: '.php_uname('r').'<br>';
echo 'Version: '.php_uname('v').'<br>';
echo 'Machine Type: '.php_uname('m').'<br>';

但是模式

s
仅返回内核类型 - Linux。

Operating System: Linux
Release Name: 2.6.32-431.29.2.el6.x86_64
Version: #1 SMP Tue Sep 9 21:36:05 UTC 2014
Machine Type: x86_64

我想知道是Fedora、CentOS还是Ubuntu等,可以吗?我也尝试过 posix_uname(),但出现错误。

致命错误:调用未定义的函数 posix_uname()

php linux operating-system linux-distro
4个回答
3
投票

在 Linux 系统上,通常有像

/etc/lsb-release
/etc/os-release
这样的文件,其中包含有关发行版的信息。

您可以在 PHP 中读取它们并提取它们的值:

if (strtolower(substr(PHP_OS, 0, 5)) === 'linux')
{
    $vars = array();
    $files = glob('/etc/*-release');

    foreach ($files as $file)
    {
        $lines = array_filter(array_map(function($line) {

            // split value from key
            $parts = explode('=', $line);

            // makes sure that "useless" lines are ignored (together with array_filter)
            if (count($parts) !== 2) return false;

            // remove quotes, if the value is quoted
            $parts[1] = str_replace(array('"', "'"), '', $parts[1]);
            return $parts;

        }, file($file)));

        foreach ($lines as $line)
            $vars[$line[0]] = $line[1];
    }

    print_r($vars);
}

(不是最优雅的 PHP 代码,但它可以完成工作。)

这会给你一个像这样的数组:

Array
(
    [DISTRIB_ID] => Ubuntu
    [DISTRIB_RELEASE] => 13.04
    [DISTRIB_CODENAME] => raring
    [DISTRIB_DESCRIPTION] => Ubuntu 13.04
    [NAME] => Ubuntu
    [VERSION] => 13.04, Raring Ringtail
    [ID] => ubuntu
    [ID_LIKE] => debian
    [PRETTY_NAME] => Ubuntu 13.04
    [VERSION_ID] => 13.04
    [HOME_URL] => http://www.ubuntu.com/
    [SUPPORT_URL] => http://help.ubuntu.com/
    [BUG_REPORT_URL] => http://bugs.launchpad.net/ubuntu/
)

ID
字段最适合确定发行版,因为它是由 Linux 标准库定义的,并且应该出现在常见发行版上。

顺便说一句,我建议不要使用

exec()
system()
来读取文件,因为出于安全原因,它们在许多服务器上被禁用。 (另外,这是没有意义的,因为 PHP 本身可以读取文件。如果它不能读取文件,那么通过系统调用也是不可能的。)


2
投票

尝试 PHP

system($call)
调用 http://php.net/manual/en/function.system.php 在那里你可以做任何你想做的事情来找到想要的信息,在 ubuntu 系统上,你可能想要使用
system('cat /etc/issue');
您可能想要使用一种方法,从 PHP 调用 bash 脚本,例如 https://unix.stackexchange.com/questions/6345/how-can-i-get-distribution-name-and-version-简单 shell 脚本中的数字


0
投票

您将需要进行系统调用,并且获取该信息的命令或位置可能因发行版而异。以下代码以级联方式查询最常见的位置,直到找到答案:

$linux_distro = null;
$os_info = file_exists("/etc/os-release") ? explode("\n", shell_exec("cat /etc/os-release")) : [];
foreach ($os_info as $line) {
    if (preg_match('/^ID=(")?[a-zA-Z]+(")?/i', trim($line))) {
        $linux_distro = strtolower(preg_replace('/(^ID=(")?|")/i', "", trim($line)));
        break;
    }
}
if (!$linux_distro) {
    $hostnamectl_available = shell_exec("command -v hostnamectl");
    $os_info = ($hostnamectl_available) ? explode("\n", shell_exec("hostnamectl")) : [];
    foreach ($os_info as $line) {
        if (preg_match('/^Operating System:\s+/i', trim($line))) {
            $linux_distro = strtolower(preg_replace('/(^Operating System:\s+)([\w]+)\b(.*)?/i', "$2", trim($line)));
            break;
        }
    }
}
if (!$linux_distro) {
    $os_info = file_exists("/etc/system-release") ? shell_exec("cat /etc/system-release") : "";
    $linux_distro = ($os_info) ? strtolower(preg_replace('/^([\w]+)\b(.*)?/', "$1", trim($os_info))) : "not found";
}

-1
投票

上面提到的可能重复的问题找出分布的成本很高。我使用 exec() 并执行命令得到了一个快速而简单的解决方案:

$cmd = 'cat /etc/*-release';
exec($cmd, $output);
print_r($output);

然后,结果出来了。

Array
(
    [0] => CentOS release 6.6 (Final)
    [1] => CentOS release 6.6 (Final)
    [2] => CentOS release 6.6 (Final)
)

鸣谢:如何:找出我的 Linux 发行版名称和版本

© www.soinside.com 2019 - 2024. All rights reserved.