Script: Running Commands on a List of Servers
Posted on In LinuxI 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:
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.