-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitwarden2xml
executable file
·43 lines (38 loc) · 2.03 KB
/
bitwarden2xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash
# bitwarden2xml - Enter bitwarden data into keepassx database:
# database=[group[<title>,icon=0,entry=[title=<name>,<username>,<password>,<url>,<comment>,<creation?>,<lastaccess?>,<lastmod?>,expire=Never]..]..]
# (<comment>: <br/> for \n, < for <, & for &; <?>: yyyy-mo-ddThh:mi:ss)
# Required: csvtool
# Usage: bitwarden2xml bitwarden.csv >keepassx.xml
[[ ! -f $1 || ! ${1: -4} = .csv ]] && echo "Need .csv file from Bitwarden" && exit 1
input=$(<"$1")$'\xd' out=&1
echo -e "<!DOCTYPE KEEPASSX_DATABASE>\n<database>" >"$out"
printf -v d '%(%Y-%m-%dT%H:%M:%S)T'
title0=
while read -r -d $'\xd'
do
[[ -z $title0 ]] && title0=$RANDOM && continue # Skip header, read next
title=$(echo "$REPLY" |csvtool col 3 -) title=${title:1}
title=${title//&/&} title=${title//</<}
name=$(echo "$REPLY" |csvtool col 4 -) name=${name:1}
name=${name//&/&} name=${name//</<}
comment=$(echo "$REPLY" |csvtool col 5 -) comment=${comment:1}
comment=${comment//&/&} comment=${comment//</<}
comment=${comment//$'\n'/<br/>} comment=${comment//\"\"/\"}
comment=${comment#\"} comment=${comment%\"} comment=${comment%<br/>}
url=$(echo "$REPLY" |csvtool col 7 -) url=${url:1}
url=${url//&/&} url=${url//</<}
username=$(echo "$REPLY" |csvtool col 8 -) username=${username:1}
username=${username//&/&} username=${username//</<}
password=$(echo "$REPLY" |csvtool col 9 -) password=${password:1}
password=${password//&/&} password=${password//</<}
if [[ ! $title = $title0 ]]
then # New group
((title0>0)) || echo -e " </group>" >>"$out"
echo -e " <group>\n <title>$title</title>\n <icon>0</icon>" >>"$out"
title0=$title
fi
echo -e " <entry>\n <title>$name</title>\n <username>$username</username>\n <password>$password</password>\n <url>$url</url>\n <comment>$comment</comment>\n <icon>0</icon>\n <creation>$d</creation>\n <lastaccess>$d</lastaccess>\n <lastmod>$d</lastmod>\n <expire>Never</expire>\n </entry>" >>"$out"
done <<<"$input"
echo -e " </group>\n</database>" >>"$out"
exit 0