-
-
Save harmy/4689160 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
usage () { | |
echo "Usage: $0 [-i [identity_file]] [user@]machine" | |
exit 1 | |
} | |
# Parse options | |
while getopts ":i:" o | |
do case "$o" in | |
i) # Identity file specified | |
if [ $(($OPTIND-1)) -eq $# ] # if there's no other argument after this one | |
then | |
# Make sure last argument is not a file or folder to catch this case where no host is provided: | |
# ssh-copy-id -i [identity_file] | |
[ ! -f "$OPTARG" ] || usage | |
[ ! -d "$OPTARG" ] || usage | |
# If last argument is not a file, read the default key and continue | |
key=`cat "$HOME/.ssh/id_rsa.pub"` # | |
else # read specified identity | |
[ -f "$OPTARG" ] || usage | |
key=`cat "$OPTARG"` | |
fi;; | |
[?]) usage;; | |
esac | |
done | |
[ $# -gt 0 ] || usage | |
shift $(($# - 1)) | |
# Show usage if no host given | |
host=$1 | |
if [ -z "$host" ] | |
then | |
usage | |
fi | |
# Default to `ssh-add -L` if no -i option given | |
if [ -z "$key" ] | |
then | |
key=`ssh-add -L` | |
fi | |
echo "Uploading private key..." | |
ssh $host 'mkdir -pm 700 ~/.ssh; echo ' $key ' >> ~/.ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys' | |
echo "Done." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment