Skip to content

Instantly share code, notes, and snippets.

@emerose
Created February 11, 2011 23:25
Show Gist options
  • Select an option

  • Save emerose/823256 to your computer and use it in GitHub Desktop.

Select an option

Save emerose/823256 to your computer and use it in GitHub Desktop.
scan an authorized_keys file and print key fingerprints
#!/usr/bin/env ruby
require 'tempfile'
KEYGEN = "/usr/bin/ssh-keygen"
KEYSFILE = "authorized_keys"
keys = []
File.open(KEYSFILE).each_line do |l|
next if (l =~ /\A\s*\Z/) # blank line
next if (l =~ /\A\s*#/) # comment line
match = l.match(/\A(ssh-[rdsa]{3})\s+(\S+=)\s+(.*)\Z/) # XXX: won't parse keys with options
raise "Couldn't parse: #{l.inspect}" unless match
keys << {:type => match[1], :key => match[2], :comment => match[3], :raw => l}
end
keys.each do |key|
f = Tempfile.open($0)
begin
f << key[:raw]
f.close
res = `#{KEYGEN} -lf #{f.path}`
raise "keygen failed on " + key[:key] + " (" + key[:comment] + ")" unless res
(bits, fingerprint, path, type) = res.split
puts key[:comment] + "\t" + fingerprint + "\t" + bits+"/"+type
ensure
f.close
f.unlink
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment