Skip to content

Instantly share code, notes, and snippets.

@dalehamel
Last active February 5, 2020 07:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalehamel/6e46bdc5276761e05847 to your computer and use it in GitHub Desktop.
Save dalehamel/6e46bdc5276761e05847 to your computer and use it in GitHub Desktop.
Ruby dmidecode
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