ed5c345aaa91b472414338ea12ecd1d482b15308
[webi-installers/.git] / ssh-utils / ssh-pubkey.sh
1 #!/bin/bash
2
3 {
4     set -e
5     set -u
6
7     if [ ! -d "$HOME/.ssh" ]; then
8         mkdir -p "$HOME/.ssh/"
9         chmod 0700 "$HOME/.ssh/"
10     fi
11
12     if [ ! -f "$HOME/.ssh/config" ]; then
13         # for the benefit of VSCode
14         touch "$HOME/.ssh/config"
15         chmod 0644 "$HOME/.ssh/config"
16     fi
17
18     if [ ! -f "$HOME/.ssh/authorized_keys" ]; then
19         touch "$HOME/.ssh/authorized_keys"
20         chmod 0600 "$HOME/.ssh/authorized_keys"
21     fi
22
23     if [ ! -f "$HOME/.ssh/id_rsa" ]; then
24         ssh-keygen -b 2048 -t rsa -f "$HOME/.ssh/id_rsa" -q -N ""
25         >&2 echo ""
26     fi
27
28     if [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
29         ssh-keygen -y -f "$HOME/.ssh/id_rsa" > "$HOME/.ssh/id_rsa.pub"
30         >&2 echo ""
31     fi
32
33     # TODO use the comment (if any) for the name of the file
34     >&2 echo ""
35     >&2 echo "~/Downloads/id_rsa.$(whoami).pub":
36     >&2 echo ""
37     rm -f "$HOME/Downloads/id_rsa.$(whoami).pub"
38     cp -r "$HOME/.ssh/id_rsa.pub" "$HOME/Downloads/id_rsa.$(whoami).pub"
39     cat "$HOME/Downloads/id_rsa.$(whoami).pub"
40     >&2 echo ""
41 }