Created
July 13, 2011 13:37
-
-
Save adorsk-whoi/1080298 to your computer and use it in GitHub Desktop.
ohai user_public_keys plugin
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.