用于检测 Linux 发行版版本的 Python 模块

发布于 2024-08-15 21:08:09 字数 252 浏览 11 评论 0原文

是否有现有的 python 模块可用于检测当前安装的 Linux 发行版以及发行版的版本。

例如:

  • RedHat Enterprise 5
  • Fedora 11
  • Suse Enterprise 11
  • 等......

我可以通过解析 /etc/redhat-release 等各种文件来制作自己的模块,但我想知道模块是否已经存在?

干杯, 伊万

Is there an existing python module that can be used to detect which distro of Linux and which version of the distro is currently installed.

For example:

  • RedHat Enterprise 5
  • Fedora 11
  • Suse Enterprise 11
  • etc....

I can make my own module by parsing various files like /etc/redhat-release but I was wondering if a module already exists?

Cheers,
Ivan

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

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

发布评论

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

评论(4

傲影 2024-08-22 21:08:09

查找平台模块的文档:http://docs.python.org/library/platform。示例

>>> platform.uname()
('Linux', 'localhost', '2.6.31.5-desktop-1mnb', '#1 SMP Fri Oct 23 00:05:22 EDT 2009', 'x86_64', 'AMD Athlon(tm) 64 X2 Dual Core Processor 3600+')
>>> platform.linux_distribution()
('Mandriva Linux', '2010.0', 'Official')

Look up the docs for the platform module: http://docs.python.org/library/platform.html

Example:

>>> platform.uname()
('Linux', 'localhost', '2.6.31.5-desktop-1mnb', '#1 SMP Fri Oct 23 00:05:22 EDT 2009', 'x86_64', 'AMD Athlon(tm) 64 X2 Dual Core Processor 3600+')
>>> platform.linux_distribution()
('Mandriva Linux', '2010.0', 'Official')
新一帅帅 2024-08-22 21:08:09

我编写了一个名为 distro 的包(现在由 pip 使用),旨在替换 distro.linux_distribution。它适用于许多发行版,这些发行版在使用 platform 时可能会返回奇怪或空的元组。

https://github.com/nir0s/distrodistro,上pypi)

它提供了一个更复杂的 API 来检索与分发相关的信息。

$ python
Python 2.7.12 (default, Nov  7 2016, 11:55:55) 
[GCC 6.2.1 20160830] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import distro
>>> distro.linux_distribution()
(u'Antergos Linux', '', u'ARCHCODE')

顺便说一句,platform.linux_distribution 将在 Python 3.7 中删除。

I've written a package called distro (now used by pip) which aims to replace distro.linux_distribution. It works on many distributions which might return weird or empty tuples when using platform.

https://github.com/nir0s/distro (distro, on pypi)

It provides a much more elaborate API to retrieve distribution related information.

$ python
Python 2.7.12 (default, Nov  7 2016, 11:55:55) 
[GCC 6.2.1 20160830] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import distro
>>> distro.linux_distribution()
(u'Antergos Linux', '', u'ARCHCODE')

By the way, platform.linux_distribution is to be removed in Python 3.7.

说不完的你爱 2024-08-22 21:08:09

上述答案不适用于 RHEL 5.x。在类似 redhat 的系统上最快的方法是读取并查看 /etc/redhat-release 文件。每次运行更新时都会更新此文件,并且系统会升级一个次要版本号。

$ python
>>> open('/etc/redhat-release','r').read().split(' ')[6].split('.')
['5', '5']

如果你把分裂的部分拿出来,它只会给你绳子。没有像您要求的那样的模块,但我认为它足够短且优雅,您可能会发现它很有用。

The above answer doesn't work on RHEL 5.x. The quickest way is on a redhat-like system is to read and look at the /etc/redhat-release file. This file is updated every time you run an update and the system gets upgraded by a minor release number.

$ python
>>> open('/etc/redhat-release','r').read().split(' ')[6].split('.')
['5', '5']

If you take the split parts out it will just give you string. No module like you asked, but I figured it was short and elegant enough that you may find it useful.

满地尘埃落定 2024-08-22 21:08:09

可能不是最好的方法,但我使用子进程执行“uname -v”,然后在输出中查找发行版名称。

import subprocess
process = subprocess.Popen(['uname','-v'], stdout=subprocess.PIPE)
stdout = process.communicate()[0]
distro = format(stdout).rstrip("\n")

if 'FreeBSD' in distro:
   print "It's FreeBSD"
elif 'Ubuntu' in distro:
   print "It's Ubuntu"
elif 'Darwin' in distro:
   print "It's a Mac"
else:
   print "Unknown distro"

Might not be the best way, but I used subprocess to execute 'uname -v' and then looked for the distro name in the output.

import subprocess
process = subprocess.Popen(['uname','-v'], stdout=subprocess.PIPE)
stdout = process.communicate()[0]
distro = format(stdout).rstrip("\n")

if 'FreeBSD' in distro:
   print "It's FreeBSD"
elif 'Ubuntu' in distro:
   print "It's Ubuntu"
elif 'Darwin' in distro:
   print "It's a Mac"
else:
   print "Unknown distro"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文