Hello everyone,

I'm trying to get a python script to run base on the os release and if its physical or virtual machine. If its virtual the script will NOT run, but if its Physical the script will run base on the os release level. I'm a little bit confusing putting the second check for virtual.

Can someone help me on this. Thank you.

#!/usr/bin/env python

import os
import platform
import sys
import os.path

os_release = platform.dist()[2]
platform = os.system("dmidecode -s system-manufacturer")

if os_release == "Santiago":
   if platform == "IBM":
     os.system("cat /etc/redhat-release")
      print platform
elif os_release == "Tikanga":
   os.system("cat /etc/redhat-release")
elif os_release == "Maipo":
   os.system("cat /etc/redhat-release")
1 Year
Discussion Span
Last Post by Gribouillis

os.system() is long deprecated, and it does not return the command's ouput. Use

import subprocess

platform = subprocess.check_output("dmidecode -s system-manufacturer", stderr=subprocess.STDOUT, shell=True)

also platform.dist() is deprecated, use platform.linux_distribution().

Edited by Gribouillis



I try your way and i'm getting this error: I use on a RH6 I also have different os releases

Python 2.6.6 (r266:84292, May 22 2015, 08:34:51)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> platform = subprocess.check_output("dmidecode -s system-manufacturer", stderr=subprocess.STDOUT, shell=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'check_output'

If you can't upgrade to python 2.7, you could perhaps run the function check_output extracted from python 2.7's subprocess module

def check_output(*popenargs, **kwargs):
    r"""Run command with arguments and return its output as a byte string.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.

    The arguments are the same as for the Popen constructor.  Example:

    >>> check_output(["ls", "-l", "/dev/null"])
    'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'

    The stdout argument is not allowed as it is used internally.
    To capture standard error in the result, use stderr=STDOUT.

    >>> check_output(["/bin/sh", "-c",
    ...               "ls -l non_existent_file ; exit 0"],
    ...              stderr=STDOUT)
    'ls: non_existent_file: No such file or directory\n'
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        raise CalledProcessError(retcode, cmd, output=output)
    return output

You need a statement such as

from subprocess import CalledProcessError, PIPE, Popen

Edited by Gribouillis

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.