ssh-copy-id-for-OSX
ssh-copy-id-for-OSX copied to clipboard
added -p for port option and added ability to have no space after -i
added -p for port option and added ability to have no space after -i
#!/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.
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
#echo ID_FILE = $ID_FILE
#echo PORT = $PORT
#echo REMOTE_SERVER = $REMOTE_SERVER
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]] [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; test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys" || exit 1
cat <<EOF
Now try logging into the machine, with "ssh '$1'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
EOF
This can also be worked around by adding quotes around it. ssh-copy-id "user@host -p 8129"
The way i did it was to create a config file and then just ssh servername
I took a slightly different approach in https://github.com/lewellyn/ssh-copy-id-for-OSX/commit/1a707e0f5f3155eb4878f383f5454e3cd59148e5 (note that my fork has other changes, as well, which I do hope are generally useful).
Note that my implementation requires spaces between the arguments and their parameters. I’ve seen that there is no consistency in this regard among third-party implementations of ssh-copy-id (I’d assume since there is no consistency with the various ssh client implementations… spaces are the only reliable way to pass options), so it feels safer to at least try to sanity check and use the options as they are documented in the man pages (which invariably show usage with spaces).
Unfortunately @chrisrink’s workaround breaks on other ssh-copy-id implementations, and my sanity dictates that my tools must at least all act somewhat similarly. And teaching users how to edit their config files (or, worse, trying to programatically do it for them!) as @jamesharrington effectively suggested just so they can send their key to a remote host they won’t open an interactive terminal to is more frustrating than a poorly-designed arcade game. There are simply too many ways for that file format to go wrong. 😞