Created
February 11, 2011 23:25
-
-
Save emerose/823256 to your computer and use it in GitHub Desktop.
scan an authorized_keys file and print key fingerprints
This file contains hidden or 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
| #!/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