6

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

例如:

  • 红帽企业 5
  • 软呢帽 11
  • 苏斯企业 11
  • ETC....

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

干杯,伊万

4

4 回答 4

19

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

例子:

>>> 平台.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+')
>>> 平台.linux_distribution()
(“Mandriva Linux”、“2010.0”、“官方”)
于 2009-12-29T22:28:32.033 回答
3

我编写了一个名为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 中删除。

于 2017-04-01T13:47:45.753 回答
2

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

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

如果你把分开的部分拿出来,它只会给你字符串。没有你问的模块,但我认为它足够简短和优雅,你可能会发现它很有用。

于 2013-07-19T18:05:04.233 回答
0

可能不是最好的方法,但我使用 subprocess 执行“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"
于 2018-05-15T19:13:42.047 回答