Skip to content

Instantly share code, notes, and snippets.

@gaishimo
Created November 17, 2013 04:08
Show Gist options
  • Save gaishimo/7509025 to your computer and use it in GitHub Desktop.
Save gaishimo/7509025 to your computer and use it in GitHub Desktop.
#!/bin/sh
if [ $# -lt 1 ]; then echo "Usage: add_ssh_public_key.sh <host> [<user>] [<port>]"; exit 1; fi
HOST=$1 USER=$2 PORT=$3
if [ -z $USER ]; then USER=`whoami`; fi
if [ -z $PORT ]; then PORT=22; fi
STATUS=`cat ~/.ssh/id_rsa.pub |
ssh -p $PORT $USER@$HOST "sh -c 'cat - >> ~/.ssh/authorized_keys; echo \\$?'"`
if [ "$STATUS" != "0" ]; then
echo "Failed ($STATUS)" 1>&2; exit $STATUS
fi
echo "public key is added to remote host."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment