Skip to content

Instantly share code, notes, and snippets.

@wezm
Created January 6, 2010 08:18
Show Gist options
  • Select an option

  • Save wezm/270131 to your computer and use it in GitHub Desktop.

Select an option

Save wezm/270131 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'csv'
if ARGV.length < 2
puts "Usage: process_export_for_lastpass.rb keychain_export.tsv lastpass_import.csv"
exit 2
end
# LastPass CSV format:
# If you have another format that LastPass doesn't currently support you can attempt to save your data as a CSV file
#
# YOU MUST MODIFY THE FIRST ROW OF THE CSV FILE FOR LASTPASS TO RECOGNIZE IT
#
# The first row's column names should be properly setup for the data in the columns
# Possible first row names:
#
# url,username,password,extra,name,grouping
#
# 'extra' means 'notes' and 'grouping' is what group it should be in. You do not have to use all the column names and they don't need to be in any order. For example your file's first row might be like:
#
# name,username,password,url
#
# If you want to import a Secure Note instead of a Site, simply ensure the 'url', 'username', and 'password' columns are left blank (or omit the 'url', 'username', and/or 'password' columns entirely), and ensure the 'extra' column is not blank. As long as these two things have been done, the record will automatically be imported as a Secure Note instead of a Site.
#
# This should be very simple, just open your saved CSV file in Excel and make sure the top row has the appropriate name from above for what it is. Save it, then import the resulting file.
# KeychainExport TSV format:
# keychainName keyName (url) account (username) password description (type) comment ¬
lasspass = CSV.open(ARGV[1], 'w')
types_to_import = ['Web form password', 'Internet password', 'secure note']
# Write headers
lasspass << %w[url username password extra name grouping]
CSV.open(ARGV[0], 'r', "\t") do |row|
_, keyname, account, password, description, _ = row
next unless types_to_import.include?(description)
# KeychainExport didn't export the where field, so the url needs to be derived from the
# keyName field. This doesn't show if its http or https though. So will just have to
# go with http for now.
# The keyName also may have the username in brackets, so detect and remove that
if keyname =~ /^([^ ]+) \([^)]*\)$/
url = "http://#{$1}"
else
url = "http://#{keyname}"
end
# Write LastPass record
if description == 'secure note'
lasspass << [nil, nil, nil, password, keyname, nil]
else
# Skip blank and "do not save" extries
next if account.nil? || account =~ /^\s*$/ || account == "Passwords\xC2\xA0not\xC2\xA0saved"
lasspass << [url, account, password, nil, keyname, nil]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment