-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlpass-atts.sh
executable file
·95 lines (72 loc) · 2.08 KB
/
lpass-atts.sh
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
##
## Usage: lpass-att-export.sh
##
##
trap cleanup 0
cleanup () {
if [[ -n "$LOGGED_IN" ]]; then lpass logout --force; fi
unset PASSWORD LPASS_DISABLE_PINENTRY LOGGED_IN
echo ""
}
LOGGED_IN=''
usage() { echo "Usage: $0 [-o <outdir>] [-i <id>]" 1>&2; exit 1; }
while getopts ":i:o:hl:" o; do
case "${o}" in
i)
id=${OPTARG}
;;
o)
outdir=${OPTARG}
;;
h)
usage
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${outdir}" ]; then
usage
fi
command -v lpass >/dev/null 2>&1 || { echo >&2 "I require lpass but it's not installed. Aborting."; exit 1; }
echo -n "Username: "
read -r USERNAME
echo -n "Password: "
read -rs PASSWORD
printf "\n\n"
export LPASS_DISABLE_PINENTRY=1
if [[ -z "$USERNAME" ]]; then echo "Failed to supply username!" && exit 1; fi
if [[ -z "$PASSWORD" ]]; then echo "Failed to supply password!" && exit 1; fi
if [ ! -d "$outdir" ]; then mkdir -p "$outdir"; fi
echo -e "\033[0;32mLog in\033[0m: starting."
LOGGED_IN=$(lpass login "$USERNAME" <<<"$PASSWORD")
if [ -z "${id}" ]; then
ids=$(lpass ls | sed -E 's/.*id:[[:space:]]([0-9]+)]/\1/')
else
ids=${id}
fi
for id in ${ids}; do
show=$(lpass show "${id}" <<<"$PASSWORD")
attcount=$(echo "${show}" | grep -c "att-")
path=$(lpass show --format="%/as%/ag%an" "${id}" <<<"$PASSWORD" | uniq | tail -1)
until [ "${attcount}" -lt 1 ]; do
att=$(lpass show "${id}" <<<"$PASSWORD" | grep att- | sed "${attcount}q;d")
attid=$(echo "$att" | cut -d ':' -f 1)
attname=$(echo "$att" | cut -d ':' -f 2 | sed -e 's/^[[:space:]]*//')
if [[ -z ${attname} ]]; then
attname=${path#*/}
fi
path=${path//\\//}
mkdir -p "${outdir}/${path}"
out=${outdir}/${path}/${attname}
if [[ -f ${out} ]]; then
out=${outdir}/${path}/${attcount}_${attname}
fi
echo "${id}" - "${path}" ": " "${attid}" "-" "${attname}" " > " "${out}"
lpass show --attach="${attid}" "${id}" --quiet > "${out}" <<<"$PASSWORD"
((attcount-=1))
done
done