-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathadd-ssh-key.sh
More file actions
executable file
·55 lines (49 loc) · 1.63 KB
/
Copy pathadd-ssh-key.sh
File metadata and controls
executable file
·55 lines (49 loc) · 1.63 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
#!/bin/bash
## Usage: ${PROGNAME} [-u|--user <username>] <public key 1> \\
## ${PROGPADD} [<public key 2> \\
## ${PROGPADD} [<public key 3....
##
## Arguments:
##
## -u, --user <username> Add SSH keys for given <username>.
## (Default: '${keyuser}'.)
##
## <public key N> Public SSH key to add to .authorized_keys.
##
##
## Usage examples:
##
## \$ ssh-add -L | xargs -d'\n' ${PROGNAME} -u \$(whoami)
## \$ ${PROGNAME} "\$(cat ~/.ssh/id_rsa.pub)"
##
## In docker context:
##
## \$ ssh-add -L | xargs -d'\n' docker exec netbsd-7.1 ${PROGNAME} -u \$(whoami)
## \$ docker exec netbsd-7.1 ${PROGNAME} "\$(cat ~/.ssh/id_rsa.pub)"
##
## Blame (most) bugs on: Martin Kjellstrand <martin.kjellstrand@madworx.se>.
: "${keyuser:=root}"
program_path="$0"
usage() {
export PROGNAME="${program_path##*/}"
export PROGPADD="${PROGNAME//?/ }"
export PROGPADD
(echo "cat <<EOT"
sed -n 's/^## \?//p' < "${program_path}"
echo "EOT") > /tmp/.help.$$ ; . /tmp/.help.$$ ; rm /tmp/.help.$$
}
# Parse command line options:
while [ "$#" -gt 0 ] ; do
case "$1" in
-u|--user) keyuser="$2" ; shift 2 ;;
-h|--help) usage ; exit 0 ;;
-*) echo "Error: Unknown option '$1'." 1>&2 ; usage ; exit 1 ;;
*) break ;;
esac
done
userhome=$(awk -F: "\$1==\"${keyuser}\" { print \$6 }" /bsd/etc/passwd)
for key in "$@" ; do
echo "Adding SSH key '${key}' to ${userhome}."
[ -d "/bsd/${keyuser}/.ssh" ] || mkdir -p "/bsd/${userhome}/.ssh "
echo "${key}" >> "/bsd/${userhome}/.ssh/authorized_keys"
done