Skip to content

Instantly share code, notes, and snippets.

@mcoms
Created January 29, 2013 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcoms/4666204 to your computer and use it in GitHub Desktop.
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.
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