3 error() { echo "$@" 1>&2; }
4 fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
8 Usage: ${0##*/} [ options ] key
10 add the key to .ssh/authorized_keys
11 give '-' to read keys from stdin
14 -p | --prefix STRING prefix key with STRING
15 -r | --replace replace any existing entries
20 local file="$1" keyline="$2"
23 sed -i "\\|$2|d" "$file" || return
26 bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
29 long_opts="help,prefix:,replace,user:"
30 getopt_out=$(getopt --name "${0##*/}" \
31 --options "${short_opts}" --long "${long_opts}" -- "$@") &&
32 eval set -- "${getopt_out}" ||
40 while [ $# -ne 0 ]; do
43 -h|--help) Usage ; exit 0;;
44 -p|--prefix) prefix="$next"; shift;;
45 -r|--replace) replace=true;;
51 [ $# -ge 1 ] || bad_Usage "must provide keys"
53 cd ~/ || fail "failed to cd ~"
54 mkdir -p -m 0755 .ssh || fail "failed to make .ssh"
56 : >> .ssh/authorized_keys ||
57 fail "can't write to ~/.ssh/authorized_keys"
59 if [ "$1" = "-" ]; then
62 # we check for success or non empty string.
63 # on final line with no EOF, it returns 1, but sets key
64 while read key || [ -n "$key" ]; do
76 removekey ".ssh/authorized_keys" "$key" || fail "failed to remove: $key"
82 echo "${prefix:+${prefix} }$key"
84 } >> .ssh/authorized_keys
85 chmod 600 .ssh/authorized_keys
87 # vi: ts=4 noexpandtab