#!/bin/bash

# Read the server list from a file
servers_file="servers.conf"
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 "mkdir -p /root/.ssh && chmod 700 /root/.ssh"
    cat $ssh_key_file | ssh -o StrictHostKeyChecking=no $server "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"