Use ansible instead
It uses eval
Appends (if not already added) a line with PUBLIC key to every of the given servers
- list - file with servers (line per server)
- ssh-key - key that will be added
- ssh
- ssh-key
- cat
- grep
Use ansible instead
It uses eval
Appends (if not already added) a line with PUBLIC key to every of the given servers
[email protected] | |
[email protected] | |
[email protected] | |
#!/bin/bash | |
eval `ssh-agent -s` && ssh-add ~/.ssh/id_rsa | |
exec_command=$(cat <<-END | |
mkdir -p ~/.ssh/ | |
touch ~/.ssh/authorized_keys | |
if grep -Fxq "$(cat ssh-key)" ~/.ssh/authorized_keys | |
then | |
echo "Already exists" | |
else | |
echo "$(cat ssh-key)" >> ~/.ssh/authorized_keys | |
fi | |
END | |
) | |
while read -r line; do | |
echo "Connecting to" $line | |
eval "ssh" $line "'$exec_command'" "< /dev/null" | |
echo "" | |
# echo -e "" >> test.txt | |
# ssh $line '' | |
done < list | |