如何在python脚本中检查系统是否是FreeBSD?

Kar*_*ter 5 python freebsd python-2.7

我想以一种python形式在2.7.x脚本中添加一个检查

if __check_freebsd__():
    # run actions which would only work on FreeBSD (e.g. create a jail)
elif __check_debian__():
    # run an alternative that runs on Debian-based systems
else:
    raise Error("unsupported OS")
Run Code Online (Sandbox Code Playgroud)

__check_freebsd__功能如何?

我已经有以下代码__check_debian__:

try:
    lsb_release_id_short = sp.check_output([lsb_release, "-d", "-s"]).strip().decode("utf-8")
    ret_value = "Debian" in lsb_release_id_short
    return ret_value
except Exception:
    return False
Run Code Online (Sandbox Code Playgroud)

所以你不必为此烦恼(当然,欢迎提出改进建议).

小智 3

文档中所述,

platform.system()
Run Code Online (Sandbox Code Playgroud)

返回平台操作系统名称,因此您可以使用它。在此线程中,您还可以看到检查底层操作系统的不同方法。