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.

3 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..

  2. Hi and thank you for providing this helpful script, I tried using this to access different routers and run 3 commands. the first command is “enable” which will take me to the enable mode so i can run the other 2 commands.

    the script is sending the first command “enable” but then it stops. it looks likes its timing-out before switching modes.

    any idea how can i make this work?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.