-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-key.sh
47 lines (39 loc) · 941 Bytes
/
add-key.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
#!/bin/bash
set -euo pipefail
fetch-ssh-private-key() {
echo "Fetching ssh-key"
set +e
lpass_ssh_key=$(lpass show --field="Private Key" "github-ssh-key")
if [[ $? -gt 0 ]]; then
echo "Failed to get github ssh-key!"
exit 1
fi
set -e
echo "${lpass_ssh_key}" > /tmp/ssh-key
chmod 0600 /tmp/ssh-key
}
seconds-until-eod() {
local now=$(date +%s)
local eod=$(date -v 17H -v 30M +%s)
seconds_until_eod=$(($eod-$now))
}
main() {
lpass status
fetch-ssh-private-key
echo "Deleting all existing ssh-keys from agent."
ssh-add -D
set +u
echo "How many hours? (leave empty to add key until: $(date -v 17H -v 30M))"
read hours
if [ -z "${hours}" ]; then
echo "Adding key until $(date -v 17H -v 30M)"
seconds-until-eod
ssh-add -t ${seconds_until_eod} /tmp/ssh-key
else
echo "Adding key for ${hours} hours"
ssh-add -t "${hours}"h /tmp/ssh-key
fi
set -u
rm /tmp/ssh-key
}
main