-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssh-copykey
executable file
·64 lines (54 loc) · 1.23 KB
/
ssh-copykey
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
#!/bin/bash
#
# Will read key from ~/.ssh/id_*.pub by default.
# Use with '-s' option to read key from stdin.
# Use with '-i' to specify an ident to use to establish the connection
print_syntax() {
echo "Syntax $0 [-s -i <id_rsa>] username@hostname"
exit 1
}
OPTIND=1
STDIN=0
IDENT=""
while getopts ":si:" opt; do
case "$opt" in
s) STDIN=1
;;
i) IDENT="-i $OPTARG"
;;
*) print_syntax
esac
done
shift $((OPTIND - 1))
if [ -z $1 ]; then
print_syntax
fi
if [ $STDIN -eq 1 ] ; then
KEY=`cat /dev/stdin`
else
declare -a keyfiles
FILES=$HOME/.ssh/id_*.pub
for f in $FILES
do
if [ -f $f ]; then
keyfiles=("${keyfiles[@]}" "$f")
fi
done
if [ ${#keyfiles[@]} -lt 1 ]; then
echo "No ssh key found..."
echo "create one with 'ssh-keygen'."
exit
elif [ ${#keyfiles[@]} -gt 1 ]; then
echo "Multiple keys found, choose one:"
select KEY in ${keyfiles[@]}
do
KEY=`cat $KEY`
break
done
else
KEY=`cat ${keyfiles[0]}`
fi
fi
echo "Uploading ssh key to $1..."
ssh -q $IDENT $* "umask 0077; mkdir -p ~/.ssh ; echo "$KEY" >> ~/.ssh/authorized_keys"
echo "done!"