Skip to content

Instantly share code, notes, and snippets.

@ianatha
Created September 9, 2015 04:12
Show Gist options
  • Save ianatha/6b09880d7796c4b7605b to your computer and use it in GitHub Desktop.
Save ianatha/6b09880d7796c4b7605b to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require "uri"
require "net/http"
require "readline"
$cache = {}
def cached_exec(cmd)
if not $cache.include?(cmd)
$cache[cmd] = `#{cmd}`.split("\n")
end
$cache[cmd]
end
def software_version()
result = cached_exec("sw_vers")
result.map { |line| line.split(":")[1].strip }.join(" ")
end
def system_profile(category, name)
cached_exec("system_profiler #{category}").grep(Regexp.new(Regexp.escape(name)))[0].split(": ")[1]
end
def users()
cached_exec("ls /Users").select { |u| u !~ /Guest/ and u !~ /Shared/ }
end
uri = URI("https://script.google.com/macros/s/AKfycbz65iB51W5lDYrpxT89trMkeBF8M2RdqkBq4J8PuftwbMp-eSs/exec")
data = {
:name => Readline.readline("What is your first name? "),
:software_version => software_version(),
:model_id => system_profile("SPHardwareDataType", "Model Identifier"),
:serial_number => system_profile("SPHardwareDataType", "Serial Number (system)"),
:processor_name => system_profile("SPHardwareDataType", "Processor Name"),
:processor_speed => system_profile("SPHardwareDataType", "Processor Speed"),
:processor_cores => system_profile("SPHardwareDataType", "Total Number of Cores"),
:memory => system_profile("SPHardwareDataType", "Memory"),
:hardware_uuid => system_profile("SPHardwareDataType", "Hardware UUID"),
:boot_rom_version => system_profile("SPHardwareDataType", "Boot ROM Version"),
:smc_version => system_profile("SPHardwareDataType", "SMC Version (system)"),
:local_time => Time.now(),
:users => users().join(";"),
:storage_capacity => system_profile("SPStorageDataType", "Capacity")
}
uri.query = URI.encode_www_form(data)
res = Net::HTTP.get_response(uri)
if not res.is_a?(Net::HTTPSuccess)
puts "Sending inventory data failed :("
else
puts "Thank you for submitting your laptop to the inventory!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment