For a short while, I found myself logging into multiple servers and anyone that is using passwords is simply doing it wrong. I was getting ready to remove password authentication on all my servers that I managed and decided to throw this simple script together to send my key to the servers before disabling passwords, I have since been using this for any new server I set up to speed up the process.
#!/bin/bash
NORMAL=$(tput sgr0)
GREEN=$(tput setaf 2; tput bold)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
function red() {
echo -e "$RED$*$NORMAL"
}
function green() {
echo -e "$GREEN$*$NORMAL"
}
function yellow() {
echo -e "$YELLOW$*$NORMAL"
}
function printhelp()
{
yellow "\nThis is a simple script that uploads your id_rsa.pub to the specified host."
yellow "Providing a passwordless login to the linux machine."
yellow ""
yellow "Use the following command to run the script."
yellow "pussh username hostname port"
}
if [ $# -lt 1 ];then
#echo "You must supply an argument!"
printhelp
exit 1;
else
if [ ! -r ${HOME}/.ssh/id_rsa.pub ]; then
ssh-keygen -b 2048 -t rsa
fi
# Append to the copy on the remote server
cat ~/.ssh/id_rsa.pub | ssh [email protected]$2 -p$3 "cat - >> .ssh/authorized_keys"
if [ $? -eq 0 ]; then
green "Success"
else
red "Error!"
fi
if [ ! -r ${HOME}/.ssh/id_dsa.pub ]; then
ssh-keygen -t dsa
fi
# Append to the copy on the remote server
cat ~/.ssh/id_dsa.pub | ssh [email protected]$2 -p$3 "cat - >> .ssh/authorized_keys"
if [ $? -eq 0 ]; then
green "Success"
else
red "Error!"
fi
# Append to the copy on the remote server
cat ~/.ssh/id_ed25519.pub | ssh [email protected]$2 -p$3 "cat - >> .ssh/authorized_keys"
if [ $? -eq 0 ]; then
green "Success"
else
red "Error!"
fi
exit 1;
fi
I have this script saved in a Scripts directory that I have set within my path, and have called it pussh for ease of use. As you can see if you already have a key you can use the following command to upload the key to the server and you should be set to go!
pussh username hostname port
If you do not happen to have a key already then it will assist you in generating one, at which point it will then proceed with sending to the specified server.