diff options
author | David Francoeur <dfrancoeur04@gmail.com> | 2015-10-20 21:32:33 -0400 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2016-02-06 20:56:47 +0100 |
commit | eafeb73852880260e14da4cf1a65515396332d5d (patch) | |
tree | 8bad294183135adaaa29071a5beb14d211c49b04 | |
parent | 35f7f2eaf307fd9f563623ceaaf88ce939cde282 (diff) |
keepass2csv2pass
The CSV is generated by KeePassX 2.0 on Mac OSX
-rwxr-xr-x | contrib/importers/keepass2csv2pass.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/contrib/importers/keepass2csv2pass.py b/contrib/importers/keepass2csv2pass.py new file mode 100755 index 0000000..47e787f --- /dev/null +++ b/contrib/importers/keepass2csv2pass.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +# Copyright 2015 David Francoeur <dfrancoeur04@gmail.com> +# This file is licensed under the GPLv2+. Please see COPYING for more information. + +# KeePassX 2+ on Mac allows export to CSV. The CSV contains the following headers : +# "Group","Title","Username","Password","URL","Notes" +# Group and Title are used to build the path, @see prepareForInsertion +# Password is the first line and the url and the notes are appended after +# +# Usage: ./csv_to_pass.py test.csv + +import csv +import itertools +import sys +from subprocess import Popen, PIPE + +def pass_import_entry(path, data): + """ Import new password entry to password-store using pass insert command """ + proc = Popen(['pass', 'insert', '--multiline', path], stdin=PIPE, stdout=PIPE) + proc.communicate(data.encode('utf8')) + proc.wait() + +def readFile(filename): + """ Read the file and proccess each entry """ + with open(filename, 'rU') as csvIN: + next(csvIN) + outCSV=(line for line in csv.reader(csvIN, dialect='excel')) + #for row in itertools.islice(outCSV, 0, 1): + for row in outCSV: + #print("Length: ", len(row), row) + prepareForInsertion(row) + + +def prepareForInsertion(row): + """ prepare each CSV entry into an insertable string """ + keyFolder = escape(row[0][4:]) + keyName = escape(row[1]) + username = row[2] + password = row[3] + url = row[4] + notes = row[5] + + path = keyFolder+"/"+keyName+"/"+username + data = password + "\n" if password else "\n" + data = "%s%s: %s\n" % (data, "url:", url+"\n") + data = "%s%s: %s\n" % (data, "notes:", notes+"\n") + pass_import_entry(path,data) + print(path+" imported!") + +def escape(strToEscape): + """ escape the list """ + return strToEscape.replace(" ", "-").replace("&","and").replace("[","").replace("]","") + + +def main(argv): + inputFile = sys.argv[1] + print("File to read: " + inputFile) + readFile(inputFile) + + +main(sys.argv) |