Created
October 31, 2014 02:18
-
-
Save vietkute02/d11b4209854d59a9feb3 to your computer and use it in GitHub Desktop.
add ssh key to remote host
This file contains 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/bash | |
# Variables | |
USER_AND_HOST=$1 | |
line="---------------------------------------------" | |
echo "REGISTERING RSA PUBLIC KEY WITH HOST: $REMOTE_HOST" | |
echo $line | |
echo "This script will prompt during the process since" | |
echo "you have not yet installed the RSA key with the" | |
echo "server. Please be patient; this should be done" | |
echo "shortly." | |
echo $line | |
# Ensure the RSA Public Key exists. | |
if [ ! -e ~/.ssh/id_rsa.pub ] | |
then | |
echo "No public SSH key found..." | |
echo "Generating SSH key. Follow the prompts:" | |
echo $line | |
ssh-keygen -t rsa | |
echo $line | |
if [ ! $? ] | |
then | |
echo "Key Generation was not successful. Exiting." | |
exit 1 | |
fi | |
fi | |
echo $line | |
echo "Ensuring remote .ssh directory exists." | |
echo "You will need to enter the remote host's password." | |
ssh $USER_AND_HOST "mkdir -p ~/.ssh/" | |
echo $line | |
echo "Copying key to remote host." | |
echo "You will need to enter the remote host's password." | |
echo $line | |
scp ~/.ssh/id_rsa.pub "$USER_AND_HOST:~" | |
echo $line | |
echo "Adding the key to the set of 'authorized_keys'..." | |
echo "You will need to enter the remote host's password." | |
ssh $USER_AND_HOST "cat id_rsa.pub >> .ssh/authorized_keys" | |
echo $line | |
echo "Cleaning up key and testing SSH access..." | |
echo "IF YOU HAVE TO ENTER A PASSWORD, IT'S FAILED!!!!" | |
ssh $USER_AND_HOST "rm ~/id_rsa.pub" | |
RESULT=$? | |
echo $line | |
if [ RESULT ] | |
then | |
echo "Your public key was successfully added to the host." | |
else | |
echo "Epic fail mate! Your key was NOT added to the host." | |
fi | |
echo $line | |
echo "Ensure you can access the server using the following command:" | |
echo "ssh $USER_AND_HOST \"ls -lah ~/.ssh\"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment