-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-gh-ssh-keys
More file actions
executable file
·117 lines (99 loc) · 2.12 KB
/
set-gh-ssh-keys
File metadata and controls
executable file
·117 lines (99 loc) · 2.12 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
PROGNAME="$(basename $0)"
show_usage() {
echo "$PROGNAME: Set SSH keys for a user"
echo "Usage: $PROGNAME [ -h, --help | -s, --show | -c, --check | -g, --generate <email>]"
return
}
check_existing_ssh_key() {
if [[ -f ~/.ssh/id_ed25519.pub ]]; then
echo "SSH key already exists."
exit 0
else
echo "No SSH key found."
exit 1
fi
}
show_ssh_key() {
if [[ -f ~/.ssh/id_ed25519.pub ]]; then
echo "Your SSH public key is:"
cat ~/.ssh/id_ed25519.pub
else
echo "No SSH key found."
fi
}
install_ssh_keygen() {
if ! command -v ssh-keygen &>/dev/null; then
echo "ssh-keygen is not installed. Please install it first. Do you want to install it now? (Y/n)"
read -r install_ssh_keygen
if [[ "$install_ssh_keygen" =~ ^[Yy]$ ]]; then
sudo apt-get install openssh-client
else
echo "ssh-keygen is required to generate SSH keys. Exiting."
exit 1
fi
fi
}
generate_ssh_key() {
install_ssh_keygen
if [[ -d ~/.ssh && -f ~/.ssh/id_ed25519.pub ]]; then
echo "SSH key already exists. Please remove it before generating a new one."
exit 1
fi
if [[ ! -d ~/.ssh ]]; then
echo "Creating .ssh directory..."
mkdir -p ~/.ssh
chmod 700 ~/.ssh
fi
echo "Generating SSH key for $email..."
ssh-keygen -t ed25519 -C "$email" -f ~/.ssh/id_ed25519 -N "" # -t is type -C is comment -f is file -N "" is no passphrase
echo "SSH key generated successfully. Your public key is:"
show_ssh_key
add_to_agent
exit 0
}
add_to_agent() {
if ! pgrep -u "$USER" ssh-agent >/dev/null ; then
eval "$(ssh-agent -s)"
fi
echo "SSH key added to the agent."
ssh-add ~/.ssh/id_ed25519
}
if [[ $# -eq 0 ]]; then
show_usage
exit 1
fi
case "$1" in
-g|--generate)
if [[ -z "$2" ]]; then
echo "Error: No email provided."
show_usage
exit 1
fi
# Check if the email is valid
if [[ ! "$2" =~ ^[^@]+@[^@]+\.[^@]+$ ]]; then
echo "Error: Invalid email format."
exit 1
fi
email="$2"
generate_ssh_key
exit 0
;;
-h | --help)
show_usage
exit 0
;;
-s | --show)
show_ssh_key
exit 0
;;
-c | --check)
check_existing_ssh_key
exit 0
;;
*)
echo "Error: Invalid option."
show_usage
exit 1
;;
esac