SSH to multiple servers and run commands


You need to run a list of commands on a list of servers and record the output of each command.


Create a perl script using Net::SSH::Perl ( a ssh client written as a perl module ). This script will read a list of commands from a file, a list of servers form another file, will connect to each server, execute each command in in the commands file then go to the next server and do the same.


Download the script: SSH Batch-0.1 (1.5 KB)
install Net::SSH::Perl

  1. sudo perl -MCPAN -e "install Net::SSH::Perl"

Now you can decompress the script and configure it

  1. tar -xzpf ssh_batch-0.1.tar.gz
  2. cd ssh_batch-0.1

put the commands in commands.txt ( one command per line )
put the servers in servers.txt (one per line ) in this format: user@hostname:port,password

Now you can test the script: run ./ and look at the output in log.txt

Warning! You can destroy multiple servers with this script!

Yeah it can do that if you're not carefull about what commands you tell it to run and you log in with a user with too  much permissions. So make sure you know what you are doing before you run it on production servers.

2 thoughts on “SSH to multiple servers and run commands

  1. Thanks for putting this together… was wondering how to send multiple commands using ssh without logging out / in several times..

Leave a Reply