Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ssh-copy-id for multiple servers
set -x
[ -f "$0" ] && SCRIPT=$0
TARGET1=$1
shift
TARGETS=$*
TARGET2=$1
for TARGET in $TARGETS
do
echo "On $HOSTNAME Copying SSH Keys $TARGET1 <=> $TARGET"
[ -f "~/.ssh/id_rsa" ] && ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub | ssh $TARGET 'mkdir .ssh; cat >> ~/.ssh/authorized_keys'
ssh-keyscan $TARGET >> ~/.ssh/known_hosts
ssh $TARGET '[ -f "~/.ssh/id_rsa" ] && ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa; cat ~/.ssh/id_rsa.pub' >> ~/.ssh/authorized_keys
ssh $TARGET "ssh-keyscan $TARGET2" >> ~/.ssh/known_hosts
[ -z "$SCRIPT" ] || ssh -t $TARGET 'bash -s' < $SCRIPT $TARGETS
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.