Skip to content

Instantly share code, notes, and snippets.

@dbadapt
Last active August 29, 2015 14:24
Show Gist options
  • Save dbadapt/7733823454166bb8bffd to your computer and use it in GitHub Desktop.
Save dbadapt/7733823454166bb8bffd to your computer and use it in GitHub Desktop.
prime_key.sh - Easily move an SSH key to a new VM or container
#!/bin/bash
# This script will convert a ssh private and public key into a
# series of commands that can be cut & paste into a VM to
# prime the logged in user in the VM with the SSH key
# Author: David Bennett - david.bennett at percona dot com - 2015-07-12
# Usage: ./prime_key.sh {key base name} {new base name}
# Example: ./prime_key.sh ~/.ssh/id_rsa vm_rsa
if [ "$1" == "" ]; then
KEY="${HOME}/.ssh/id_rsa"
else
KEY="$1"
fi
if [ "$2" == "" ]; then
NEWKEY="id_rsa"
else
NEWKEY="$2"
fi
# shellcheck disable=SC2086
if [ ! -f ${KEY} ] || [ ! -f ${KEY} ]; then
echo "Could not find ${KEY} or ${KEY}.pub"
exit 1;
fi
# echo the commands
cat <<_EOF_
# --- start copy ---
cd ~
mkdir -p .ssh
chmod 700 .ssh
cd .ssh
cat <<_EOKEY_ > ${NEWKEY}
_EOF_
# shellcheck disable=SC2086
cat ${KEY}
cat <<_EOF_
_EOKEY_
chmod 600 ~/.ssh/${NEWKEY}
cat <<_EOPUB_ > ${NEWKEY}.pub
_EOF_
# shellcheck disable=SC2086
cat ${KEY}.pub
cat <<_EOF_
_EOPUB_
chmod 600 ~/.ssh/${NEWKEY}.pub
cd ~
# --- end copy ---
_EOF_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment