Skip to content

Instantly share code, notes, and snippets.

@rbranson
Created June 23, 2009 17:51
Show Gist options
  • Select an option

  • Save rbranson/134713 to your computer and use it in GitHub Desktop.

Select an option

Save rbranson/134713 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# sendkey.sh: Helps proliferate your SSH public key to hosts.
#
# Rick Branson <rick@diodeware.com>
# This code is released into the public domain.
#
if [ "$#" -lt 1 ]; then
echo "sendkey.sh: Sends id_rsa.pub as authorized_keys to remote host (over SSH)"
echo "Usage: sendkey.sh <[user@]host>"
exit 2
fi
if [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
echo "You don't have an id_rsa.pub file, use ssh-keygen to create one!"
exit 3
fi
echo
echo "=== PUSHING FILES ==="
echo
echo "You will be prompted to enter your password below to push your"
echo "authorized_keys file to the remote host."
echo
PUBLIC_KEY=`cat $HOME/.ssh/id_rsa.pub`
ssh $1 <<__EOT__
echo ""
if [ ! -d "\$HOME/.ssh" ]; then
echo "REMOTE: No .ssh directory, creating it..."
mkdir -p \$HOME/.ssh
chmod 755 \$HOME/.ssh
fi
echo "REMOTE: Adding key to authorized_keys file..."
echo "$PUBLIC_KEY" >> \$HOME/.ssh/authorized_keys
__EOT__
echo
echo "=== TESTING ==="
echo
echo "If you get an error below, something went wrong!"
echo
ssh -o "PasswordAuthentication=no" $1 "echo \"... but it didn't go wrong, it worked!\""
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment