Skip to content

Instantly share code, notes, and snippets.

@dalehamel

dalehamel/dmi.rb

Last active Feb 5, 2020
Embed
What would you like to do?
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