-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeygen
executable file
·66 lines (54 loc) · 1.4 KB
/
keygen
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
#!/usr/bin/env bash
set -Eeuo pipefail
BASE_DIR=$(dirname "${BASH_SOURCE[0]:-$0}")
cd "${BASE_DIR}/.." || exit 127
# shellcheck source=./helpers.sh
. helpers.sh
PROGRAM=$(basename "${BASH_SOURCE[0]:-$0}")
VERSION=0.9.0
function display_help() {
cat <<EOF
$(help_section Usage)
${PROGRAM} [OPTIONS] rsa [<comment>]
$(help_section Options)
-h, --help Show this screen.
-v, --version Show version.
$(help_section Examples)
${PROGRAM} rsa email@example.com
EOF
}
function generate_new_key() {
local COMMENT=$1
local SSH_PUB="$HOME/.ssh/id_rsa.pub"
#TODO: add select to generate multiple key types
ssh-keygen -t rsa -b 4096 -C "$COMMENT"
if command -v xclip >/dev/null 2>&1; then
xclip -selection clipboard <"$SSH_PUB"
elif command -v pbcopy >/dev/null 2>&1; then
pbcopy <"$SSH_PUB"
elif command -v xsel >/dev/null 2>&1; then
xsel --clipboard <"$SSH_PUB"
elif command -v wl-copy >/dev/null 2>&1; then
wl-copy <"$SSH_PUB"
else
echo "[ssh]: xclip, pbcopy, xsel and wl-copy not found, you need to copy manually the following key"
cat "$SSH_PUB"
exit 1
fi
echo "[ssh]: SSH public key copied into the clipboard"
}
case ${1:-error} in
-h | --help)
display_help
;;
-v | --version)
display_version "$PROGRAM" "$VERSION"
;;
rsa)
generate_new_key "${2:-$(whoami)@$(uname -n)}"
;;
*)
display_help >&2
exit 1
;;
esac