-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkey-add
executable file
·46 lines (35 loc) · 1.15 KB
/
key-add
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
#!/bin/sh
homeGit=/home/git
RED='\033[0;31m'
GRE='\033[0;32m'
YEL='\033[0;33m'
NC='\033[0m' # No Color
# Read in the SSH key
echo Input the key to be added:
read key
# Place the key in a temporary file (it's hard to get ssh-keygen
# to read from stdin; <<< works for bash, but is non-posix)
keyfile=$(mktemp) &&\
echo "$key" > $keyfile
# Get user from key comment
user=$(echo "$key"| cut -d" " -f3)
# Generate a fingerprint
fingerprint=$(ssh-keygen -lf $keyfile |cut -d" " -f1,2)
# Check for errors
if [ $(echo "$fingerprint" | egrep -c '[0-9]+ ([0-9A-Fa-f]{2}:){15}[0-9A-Fa-f]{2}') -eq 0 ]
then
# Display the fingerprint error and clean up
echo "Error: $fingerprint"
rm $keyfile
exit 1
fi
# Add the key to the authorised keys file and clean up
mkdir -p ${homeGit}/.ssh &&\
echo -n "no-agent-forwarding,no-port-forwarding,no-X11-forwarding " >> ${homeGit}/.ssh/authorized_keys &&\
cat $keyfile >> ${homeGit}/.ssh/authorized_keys
rm $keyfile
# Display the fingerprint for reference
echo "Success!"
echo -e "Added a key with the following fingerprint:${GRE} 4096 c8:25:69:4e:ae:49:5b:83:47:d6:d6:b0:c1:99:0f:06${NC}"
echo -e "User: ${GRE}$user${NC}"
exit 0