Skip to content

Instantly share code, notes, and snippets.

@labocho
Created October 27, 2015 05:12
Show Gist options
  • Select an option

  • Save labocho/16bd961426dc8908699f to your computer and use it in GitHub Desktop.

Select an option

Save labocho/16bd961426dc8908699f to your computer and use it in GitHub Desktop.
Convert exported XML file from KeepassX that could be imported to 1Password (via CSV comma separated file)
#!/usr/bin/env ruby
# Usage: ruby keepassx-to-1passwords.rb keepassx.xml > 1password.csv
# Inspired by: Convert exported XML file from KeepassX that could be imported to 1Password (via CSV comma separated file)
# https://gist.github.com/arudmin/52aec759a8c5d7543e36#file-keepassx-to-1passwords-py
require "csv"
require "nokogiri"
def each_entry(group, super_group_title = nil, &block)
group_title = group.xpath("title").first.text
group_title = super_group_title + " - " + group_title if super_group_title
group.xpath("entry").each do |entry|
# Reference: Advanced import: creating CSV files - 1Password for Mac Knowledgebase - version 5
# https://guides.agilebits.com/1password-mac-kb/5/en/topic/advanced-import-csv
#
# For Login items
# title, URL, username, password, notes, custom field 1, custom field 2, … ,custom field N
# …where “custom field X” is an optional field that can be imported.
yield [
entry.xpath("title")[0].text,
entry.xpath("url")[0].text,
entry.xpath("username")[0].text,
entry.xpath("password")[0].text,
entry.xpath("comment")[0].text,
group_title, # group
]
end
group.xpath("group").each do |subgroup|
each_entry(subgroup, group_title, &block)
end
end
csv = CSV.new($stdout)
doc = Nokogiri.parse(ARGF.read)
doc.xpath("/database/group").each do |group|
each_entry(group) do |row|
csv << row
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment