0

I'm running a dmidecode in linux to get the list of hardware information. What is the best way to read over the output and select certain bits of information? For example get the Product Name: part of the dmidecode?

At the moment I'm writing the subprocess output to a file then reading over the file for a given string. This seems such an inefficient way of doing things.

Also I know about the python dmidecode model but for the life of me I can't get it working it just keeps saying there's no bios attribute

twigg
  • 3,753
  • 13
  • 54
  • 96
  • @J.F.Sebastian how is this marked as a duplicate? The linked question only shows how to store the output of a subprocess, I know how this is done, I wanted the best way to get a certain part of the subprocess output. The duplicate brigade on here lately are ridiculous, if you've got nothing better to do at least read the question – twigg Jul 13 '15 at 14:53
  • your question has *no* indication that you know how to store the output of a subprocess. [edit] it, to include this info: to show that your question is different. What is your question? How to find a line that contains `'Product Name'` in a bytestring or a list of strings? – jfs Jul 13 '15 at 14:58
  • "What is the best way to read over the output and select certain its of information? For example get the Product Name: part of the dmidecode?" I think its pretty clear in the question i wanted to select a certain part of it – twigg Jul 13 '15 at 15:00
  • I still see *"no indication that you know how to store the output of a subprocess"*. Is it a regex question? Do you want `re.findall(br'Product Name: (.*?)$', output, re.M)`? – jfs Jul 13 '15 at 15:09

2 Answers2

2

If you know the specific keyword you are looking for you can type: dmidecode -s keyword

In your case it would be:

dmidecode -s system-product-name

You can also filter by type. For example:

  • To return System information:

    dmidecode -t1
    
  • To return BaseBoard information:

    dmidecode -t2 
    
  • To return Chassis Information:

    dmidecode -t3
    
stark
  • 12,615
  • 3
  • 33
  • 50
  • do you have a list of all the items in the dmidecode such as system-product-name? – twigg Jul 13 '15 at 14:56
  • Yes you can find it from the man. Here is the list : bios-vendor, bios-version, bios-release- date, system-manufacturer, system-product-name, system-version, system-serial-number, system-uuid, baseboard-man- ufacturer, baseboard-product-name, baseboard-version, baseboard-serial-number, baseboard-asset-tag, chassis-manu- facturer, chassis-type, chassis-version, chassis-serial-number, chassis-asset-tag, processor-family, processor- manufacturer, processor-version, processor-frequency – Mathieu Coavoux Jul 13 '15 at 15:03
  • 1
    @twigg: run `dmidecode -s` to get the full list – jfs Jul 13 '15 at 15:04
1

There are multiple ways with which you can get the output of the command in your python script using subprocess module.

  1. subprocess.Popen() - you can start the command line process using this Popen class specifying stdout as subprocess.PIPE and then use communicate function to get the results. Example -

    import subprocess
    p = subprocess.Popen(['dmidecode'] , stdout=subprocess.PIPE)
    result = p.communicate()[0]
    
  2. subprocess.check_output() - this function returns the output of the command (output to stdout) as a byte string after executing the command. Example -

    import subprocess
    result = subprocess.check_output(['dmidecode'])
    

For your particular case, subprocess.check_output() is most probably more suited as you do not need to provide any inputs to the process.

With subprocess.Popen() you can also need to provide inputs to the process , by PIPING the stdin for the process.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • why would you show `Popen()` + `.communicate()` if there is `check_output()`? – jfs Jul 13 '15 at 14:39
  • I just gave some possibilities. They can choose whatever they want. – Anand S Kumar Jul 13 '15 at 14:41
  • you haven't specified any criteria on why one should be more preferable than the other. Why these 2 specific methods? there are many other methods exist, I don't see them being mentioned -- why stop? – jfs Jul 13 '15 at 14:49
  • I specified the methods I knew about. If you know about more methods, please do tell me. – Anand S Kumar Jul 13 '15 at 14:54
  • There are many methods that can be used in different cases (do you need to use the output *before* subprocess exits (`iter(p.stdout.readline, b'')`); do you need the same behavior as in the terminal (pexpect, pty); do you need to get the output in a GUI application (threads, async.io); do you need to capture both stdout and stderr separately (`.communicate()`), etc -- too many to list them all). I see no reason to mention other methods unless there is a specific reason to prefer something else in this particular case. – jfs Jul 13 '15 at 15:21