-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathssh-keygen.sh
executable file
·103 lines (80 loc) · 2.03 KB
/
ssh-keygen.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
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash
# Interactivly create SSH keypairs with my preferences.
# # NOTE don't use alias for github.com since lot's of applications will break like git submodule and vundle. Just use 'Host "*github.com"'.
is_macos() {
[[ "$OSTYPE" == "darwin"* ]]
}
only_key=n
test "$1" = "--only-key" && only_key=y
alias=""
while [ -z "$alias" ]; do
echo -n "Alias the remote the host: "
read alias
done;
hostname=""
while [ -z "$hostname" ]; do
echo -n "Hostname for the remote the host: "
read hostname
done;
echo -n "Port nummer [22]: "
read port
if [ -z "$port" ]; then
port="22"
fi
echo -n "Username for the remote the host [${USER}]: "
read user
if [ -z "$user" ]; then
user="$USER"
fi
echo -n "Encryption algorithm [-t ed25519]: "
read algo
if [ -z "$algo" ]; then
algo="ed25519"
fi
echo -n "Additonal comments? (-C): "
read comments
if [ -n "$comments" ]; then
comments=": ${comments}"
fi
key_stem="${alias}_id_${algo}"
cmd_keygen="ssh-keygen -t ${algo} -f \$HOME/.ssh/identityfiles/${key_stem} -C \"${USER}@${HOSTNAME} for ${user}@${alias}${comments}\""
echo "$cmd_keygen"
echo -n "OK [Y/n]: "
read ok
if !([ -z "$ok" ] || [[ "$ok" = [yY] ]]); then
exit
fi
if ! [ -d $HOME/.ssh ]; then
mkdir $HOME/.ssh
chmod 700 $HOME/.ssh
fi
if ! [ -d $HOME/.ssh/identityfiles ]; then
mkdir $HOME/.ssh/identityfiles
chmod 700 $HOME/.ssh/identityfiles
fi
eval "$cmd_keygen"
test "$only_key" = "y" && exit
ssh-config-create.sh
cat << EOF >> $HOME/.ssh/config
Host ${alias}
Hostname ${hostname}
Port ${port}
User ${user}
IdentityFile ~/.ssh/identityfiles/${key_stem}
EOF
cmd_copy="ssh-copy-id -i \$HOME/.ssh/identityfiles/${key_stem}.pub ${user}@${hostname} -p ${port}"
echo "$cmd_copy"
echo -n "[Y/n]: "
read ok
if ([ -z "$ok" ] || [[ "$ok" = [yY] ]]); then
eval "$cmd_copy"
fi
is_macos && apple_keychain=--apple-use-keychain || apple_keychain=
cmd_agent="ssh-add ${apple_keychain} \$HOME/.ssh/identityfiles/${key_stem}"
echo "$cmd_agent"
echo -n "[Y/n]: "
read ok
if ([ -z "$ok" ] || [[ "$ok" = [yY] ]]); then
eval $(ssh-agent)
eval "$cmd_agent"
fi