Skip to content

Instantly share code, notes, and snippets.

@adorsk-whoi
Created July 13, 2011 13:37
Show Gist options
  • Save adorsk-whoi/1080298 to your computer and use it in GitHub Desktop.
Save adorsk-whoi/1080298 to your computer and use it in GitHub Desktop.
ohai user_public_keys plugin
provides 'user_public_keys'
require 'etc'
# Initialize mash.
user_public_keys Mash.new
# For each entry in the password file...
Etc.passwd do |entry|
# If the entry has a home dir...
if ! entry.dir.empty?
# Make the path to the expected ssh folder.
ssh_folder = "#{entry.dir}/.ssh"
## If the ssh folder exists...
if File.directory?(ssh_folder)
# Initialize a mash to hold the entry's key info.
entry_keys = Mash.new
# Get the public key files.
public_key_files = Dir.glob("#{ssh_folder}/*.pub")
# For each public key file...
public_key_files.each do |key_file|
# Try to save the key file contents to the mash.
begin
entry_keys[File.basename(key_file)] = IO.read(key_file)
# Pass if we couldn't read the file.
rescue
end
# If we got key data, save it to the main mash.
if ! entry_keys.empty?
user_public_keys[entry.name] = entry_keys
end
end
end
end
end
@adorsk-whoi
Copy link
Author

Test script: you can test this with the script below. Put the plugin in a folder called 'plugins' (with nothing else in it except other plugins), and add the plugins path per the script below.

#!/usr/bin/ruby

require 'rubygems'
require 'ohai'

Ohai::Config[:plugin_path] << './plugins'

o = Ohai::System.new
o.all_plugins

puts o.user_public_keys.inspect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment