Last active
February 5, 2020 07:15
-
-
Save dalehamel/6e46bdc5276761e05847 to your computer and use it in GitHub Desktop.
Ruby dmidecode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
def standardize_key(k) | |
k.downcase.gsub(/\s+/,'_') | |
end | |
def read_dmi | |
data=`dmidecode` | |
dict={} | |
handle = 0 | |
current_title = nil | |
data.lines.each do |line| | |
case line | |
when /^End Of Table/, /^\s+$/, /^\# dmidecode/, /^SMBIOS/, /structures occupying/, /^Table at/ | |
next | |
when /^Handle\s+(.*?),\s+/ | |
handle = $1.to_i(16) | |
when /(.*)\s+Information\n$/, /(.*)\s+Device\n$/, /(.*)\s+Device Mapped Address\n$/, /(.*)\s+Array Mapped Address\n$/, /Physical\s+(.*)\s+Array\n$/ | |
title = standardize_key($1) | |
current_title = title | |
dict[title] ||= [] | |
dict[title] << {'handle' => handle} | |
else | |
raw_data = line.strip.split(':') | |
if raw_data.is_a?(Array) && raw_data.length == 2 | |
k, v = raw_data | |
dict[current_title].last[standardize_key(k)] = v.strip | |
end | |
end | |
end | |
dict | |
end | |
puts JSON.pretty_generate(read_dmi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment