Skip to content

Instantly share code, notes, and snippets.

@elico
Last active February 16, 2025 09:30
Show Gist options
  • Save elico/8d6f48475dc5640e4dc9904aca281176 to your computer and use it in GitHub Desktop.
Save elico/8d6f48475dc5640e4dc9904aca281176 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
IP_LIST_FILE="ips"
KEYS_FILE="keys.txt"
SSH_PASS="$(head -1 ssh_password)"
SSH_USER="root"
SSH_PORT="22"
mapfile -t keys < "${KEYS_FILE}"
mapfile -t ips < "${IP_LIST_FILE}"
for key in "${keys[@]}"; do
SSH_KEY_TMP_FILE="$(mktemp)"
chmod 600 "${SSH_KEY_TMP_FILE}"
echo "${key}" > "${SSH_KEY_TMP_FILE}"
for ip in "${ips[@]}"; do
sshpass -p "${SSH_PASS}" ssh-copy-id -i "${SSH_KEY_TMP_FILE}" \
"${SSH_USER}@${ip}" -p "${SSH_PORT}"
done
rm -vf "${SSH_KEY_TMP_FILE}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment