Executar comando para servidores remotos

Script bash simples para uma vida fácil.

remote.sh

#!/bin/bash
echo
"==> start $0";
for server in `cat remote_servers.txt`
do
echo
-e "rn==> connect to [$server]";
echo
"RUN command [$1]";
ssh root@$server
"$1"
if [[ $? == 2 ]]; then
echo
"ERROR in command [$*]";
fi;
done;
echo
"==> end $0";

remote_servers.txt

example1.com
example2
.com

Exemplos de uso:

Exemplo 1

./remote.sh "tail -n 10 /var/log/nginx/error.log"

Exemplo 2

./remote.sh "cd /var/www/v1.0.3.example.com; git pull origin production;"
./remote.sh "
rm /var/www/example.com; ln -s /var/www/v1.0.3.example.com/ /var/www/example.com;"