是否有现有的python模块可用于检测Linux的哪个发行版以及当前安装的发行版的哪个版本。
例如:
11
我可以通过解析/etc/redhat之类的各种文件来创建自己的模块,但是我想知道模块是否已经存在了?
干杯,伊凡
发布于 2017-04-01 13:47:45
我编写了一个名为distro (现在由pip使用)的包,目的是取代distro.linux_distribution。它可以在许多发行版上工作,这些发行版在使用platform时可能返回奇怪的或空的元组。
https://github.com/nir0s/distro (distro,on 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将在Python3.7中删除。
发布于 2013-07-19 18:05:04
上述答案在RHEL 5.x上不起作用。在类似红帽的系统上,最快的方法是读取和查看/etc/redhat释放文件。此文件每次运行更新时都会更新,并且系统将通过一个次要的发布号进行升级。
$ python
>>> open('/etc/redhat-release','r').read().split(' ')[6].split('.')
['5', '5']如果你把分割出来的部分拿出来,它只会给你字符串。没有像您所要求的模块,但我认为它足够简短和优雅,您可能会发现它很有用。
发布于 2018-05-15 19:13:42
可能不是最好的方法,但我使用子进程执行'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"https://stackoverflow.com/questions/1977306
复制相似问题