Tag Archives: batch

SSH to multiple servers and run commands

Problem

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

Solution

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.

Installation

Download the script: [download#8]
install Net::SSH::Perl

  1. span style="color: #ff0000;">"install Net::SSH::Perl"

Now you can decompress the script and configure it

  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 ./ssh_batch.pl 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.