Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether the system is FreeBSD in a python script?

I would like to add a check in a python 2.7.x script in the form of

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")

How would the __check_freebsd__ function look like?

I have the following code for __check_debian__ already:

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

So you don't have to bother with it (suggestions for improvements are welcome, of course).

like image 380
Kalle Richter Avatar asked May 03 '15 15:05

Kalle Richter


3 Answers

As stated in documentation,

platform.system()

returns the platform OS name, so you can use this. In this thread you can see also different approaches to check the underlying OS.

like image 181
Adalee Avatar answered Nov 08 '22 20:11

Adalee


Look at os.uname.

I'm not 100% certain, but it would probably be something like os.uname()[0] == "FreeBSD".

like image 26
orlp Avatar answered Nov 08 '22 20:11

orlp


Try this:

>>> from sys import platform
>>> platform()
# on my system I get
'linux' # check string for freebsd

Also:

# below are my results
>>> import platform
>>> platform.system()
'Linux' # would be 'FreeBSD' if I was using that
>>> platform.platform()
'Linux-3.19.0-15-generic-x86_64-with-Ubuntu-15.04-vivid'
like image 1
Totem Avatar answered Nov 08 '22 20:11

Totem