Skip to content

Instantly share code, notes, and snippets.

@dinkopehar
Created July 19, 2020 21:20
Show Gist options
  • Save dinkopehar/61f1d1c2dcc758720cdc3d5296d48d71 to your computer and use it in GitHub Desktop.
Save dinkopehar/61f1d1c2dcc758720cdc3d5296d48d71 to your computer and use it in GitHub Desktop.
Copy your public key to remote machine for ssh without password
#!/usr/bin/env bash
### Script to store ssh_key on remote machine.
### No prompt for password :)
# Replaced with ssh-copy-id :shrug:
if [[ "$#" -ne 1 ]]; then
echo "Interactive help:"
echo "-----------------"
echo " --- Script to store public ssh key to remote machine ---"
echo " > ./prepare_env [IP]"
echo "-----------------"
echo "Example:"
echo " > ./prepare_env 172.20.13.4"
echo " > ./prepare_env 172.20.13.213"
echo ""
exit 1
fi
CHECK_SSH=$(cat ~/.ssh/id_rsa.pub &> /dev/null)
if [[ $? -eq 1 ]]; then
echo "Please create SSH key."
exit 1
fi
USERNAME='root'
read -s -p "Enter remote's $USERNAME password: " ROOTPASS
sshpass -p ${ROOTPASS} ssh ${USERNAME}@$1 mkdir -p .ssh
PUBLIC_KEY=$( cat ~/.ssh/id_rsa.pub )
sshpass -p ${ROOTPASS} ssh ${USERNAME}@$1 "echo ${PUBLIC_KEY} >> .ssh/authorized_keys"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment