Script: Running Commands on a List of Servers

I frequently run some commands/scripts on a batch of servers whose IPs are already collected in a file. I usually use a simple loop and call the ssh to run these commands remotely and automatically. This operation, however, is common and can be abstracted into a separate script.

Here is the script that I come up with for running commands automatically on a batch of servers:

batch-cmd.sh.

The usage:

usage: ./batch-cmd.sh file cmd
Each host in one line in the file

The mechanism is to use ssh to run a command remotely. For example, to run command “ls” on remote host $host, we just run

ssh $host "ls"

If the current user can log on $host without password, it is automatically executed.

Eric Zhiqiang Ma

Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *