Last Updated: February 25, 2016
·
174
· islandfuture

Run command to remotes servers

Simple bash script for easy life.

remote.sh

#!/bin/bash
echo "==> start $0";    
for server in `cat remote_servers.txt`
do
    echo -e "\r\n==> 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

Examples for use:

Example 1

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

Example 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;"