28 lines
801 B
Bash
28 lines
801 B
Bash
#!/bin/bash
|
|
|
|
# Read the server list from a file
|
|
servers_file="servers.txt"
|
|
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"
|