Created
January 29, 2013 18:00
-
-
Save mcoms/4666204 to your computer and use it in GitHub Desktop.
This ruby script reads a KeePass XML export (v2) and inserts the entries into pass, the UNIX password manager. It will use a path starting with "KeePass/" so you can manually check the entries.
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
def find_path(entry) | |
path = [] | |
until entry.parent.name == 'Root' do | |
entry = entry.parent | |
path.insert(0, entry.css('>Name').text) | |
end | |
path | |
end | |
def insert_entry(entry) | |
attributes = Hash[entry.css('>String').map {|s| [s.css('Key').text, s.css('Value').text] }] | |
return unless attributes.has_key?('Title') && attributes.has_key?('Password') | |
path = find_path entry | |
path[0] = 'KeePass' | |
file = path.join('/') << '/' << attributes['Title'] | |
puts "Saving: #{file}" | |
output = "#{attributes['Password']}" | |
output << "\nUser: #{attributes['UserName']}" if attributes.has_key? 'UserName' | |
attributes.delete 'Password' | |
attributes.delete 'UserName' | |
unless attributes.empty? | |
output << "\n" | |
attributes.each {|k,v| output << "\n#{k}: #{v}" } | |
end | |
ap `echo "#{output}" | pass insert -f -m #{file.gsub(' ', '\ ').gsub('&','-').gsub('\'','')}` | |
end | |
require 'nokogiri' | |
doc = Nokogiri::XML(File.open 'keepass.xml') | |
doc.css('Group>Entry').each {|e| insert_entry e } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment