I work on multiple servers through ssh. I have automated the copying of ssh auth keys with the script below.
The only problem is it asks for the remote servers password 4 times.
How can I change this to ask just once.
#!/usr/bin/env bash
#
# Usage:
# copy-ssh-key.sh example.com
#
echo -e "\n>>> Creating remote .ssh folder"
ssh -t $1 "mkdir ~/.ssh"
echo -e "\n>>> Updating .ssh folder permissions"
ssh -t $1 "chmod 700 ~/.ssh"
echo -e "\n>>> Copying Auth Keys"
cat ~/.ssh/id_rsa.pub | ssh $1 "cat >> ~/.ssh/authorized_keys"
echo -e "\n>>> Updating auth keys permissions"
ssh -t $1 "chmod 600 ~/.ssh/authorized_keys"
Best Answer
Remember that SSH on the remote server uses your shell (e.g.
bash
) to run the commands. So this will work:(Because the commands prior to
cat
don't read from STDIN,cat
gets the input.)