-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-copy-id
executable file
·77 lines (65 loc) · 1.76 KB
/
ssh-copy-id
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
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh
# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.
#
# Modified from https://github.com/beautifulcode/ssh-copy-id-for-OSX
ID_FILE="${HOME}/.ssh/id_rsa.pub"
for arg in "$@"
do
case "$arg" in
-i*)
ID_FILE=`echo $arg | sed 's/-i//g'`
LAST=idfile
continue
;;
-p*)
PORT=`echo $arg | sed 's/-p//g'`
LAST=port
continue
;;
*)
if [ "$LAST" == "idfile" ] && [ 0 == `expr "$arg" : ^-` ] ; then
ID_FILE=$arg
elif [ "$LAST" == "port" ] ; then
PORT=$arg
else
REMOTE_SERVER=$arg
fi
LAST=""
esac
done
if [ -n "$ID_FILE" ]; then
if expr "$ID_FILE" : ".*\.pub" > /dev/null ; then
ID_FILE="$ID_FILE"
else
ID_FILE="$ID_FILE.pub"
fi
else
if [ x$SSH_AUTH_SOCK != x ] ; then
GET_ID="$GET_ID ssh-add -L | grep -vxF 'The agent has no identities.'"
fi
fi
if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
GET_ID="cat ${ID_FILE}"
fi
if [ -z "`eval $GET_ID`" ]; then
echo "$0: ERROR: No identities found" >&2
exit 1
fi
if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ -z "$REMOTE_SERVER" ]; then
echo "Usage: $0 [-i [identity_file]] [-p port] [user@]machine" >&2
exit 1
fi
if [ -n "$PORT" ] ; then
PPORT="-p$PORT"
fi
{ eval "$GET_ID" ; } | ssh $REMOTE_SERVER $PPORT "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1
SSH_CMD="ssh $1"
if [ -n "$PORT" ] ; then
SSH_CMD="ssh $1 -p $PORT"
fi
cat <<EOF
Now try logging into the machine with "$SSH_CMD".
EOF