-
Notifications
You must be signed in to change notification settings - Fork 1
/
addsshkey
executable file
·45 lines (37 loc) · 970 Bytes
/
addsshkey
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
#!/usr/bin/env bash
set -e
USAGE="addsshkey [-h] [USER KEY] -- add a new ssh key to an account.
Creates and chmods .ssh if it doesn't exist, appends key to ~/.ssh/authorized_keys
-h show this help text
USER the account username
KEY the ssh key (in quotes, including ssh-rsa)"
# No options, show usage
if [ $# == 0 ] ; then
echo "$USAGE"
exit 0;
fi
# options
while getopts 'h' option; do
case "$option" in
h) echo "$USAGE"
exit 0
;;
esac
done
# no options, use two arguments
username=$1
ssh_key=$2
# become user, create ssh directory and add key
echo "Becoming ${username}, creating ssh dir and adding key"
become_output=$(sudo /shared/ucl/sysops/libexec/become $username <<EOF
echo "Beacon"
mkdir -p .ssh
chmod go-rwx .ssh
echo "${ssh_key}" >> ~/.ssh/authorized_keys
EOF
)
become_exit_status=$?
if [ "${become_output:0:6}" != "Beacon" ]; then
echo "Error: could not become user ${username}" >&2
exit 1
fi