batchupdate/setup-servers

28 lines
802 B
Plaintext
Raw Normal View History

2023-04-19 20:24:33 +02:00
#!/bin/bash
# Read the server list from a file
2023-04-19 20:25:12 +02:00
servers_file="servers.conf"
2023-04-19 20:24:33 +02:00
if [ ! -f "$servers_file" ]; then
echo "Error: $servers_file does not exist."
exit 1
fi
servers=($(cat "$servers_file"))
# Path to the SSH key file
ssh_key_file="/root/.ssh/id_rsa.pub"
# Loop through servers and add the SSH key to the authorized_keys file
for server in "${servers[@]}"
do
echo "Adding SSH key to $server"
ssh -o StrictHostKeyChecking=no $server "sudo mkdir -p /root/.ssh && sudo chmod 700 /root/.ssh"
cat $ssh_key_file | ssh -o StrictHostKeyChecking=no $server "sudo tee -a /root/.ssh/authorized_keys > /dev/null"
if [ $? -eq 0 ]; then
echo "SSH key added to $server"
else
echo "Failed to add SSH key to $server"
fi
done
echo "SSH keys added to all servers"