Jump to content

Multi-Exec - A helpful free bash script to execute scripts on multiple servers

- - - - -

  • Please log in to reply
No replies to this topic

#1
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
So ive worked for a few linux based companies where I need to execute basic scripts on multiple servers, I made a neat script that would take a list in a text file, then scp the script to them then execute it, well every time I go to a new company, I need to change it a little bit. So I went home a while ago, had a few beers, and perfected it.

I realize there are plenty of applications that do this, such as puppet, chef, etc, but this is just a basic one, very simple, takes seconds to setup and execute.

Download:
[B][I]http[/I][/B]://linux-multi-exec.googlecode.com/svn/trunk/ linux-multi-exec-read-only

Code: linux-multi-exec - Revision 28: /trunk

Features:
  • Very secure, uses SCP and SSH and RSA keys, no passwords
  • Very diverse, the script has many options you can use to help you debug issues, or just know whats going on
  • Works on all *nix that ive tested (all debian, centos, redhat... Test it more for me and let me know)
  • Awesome logging (if enabled)

Example

Lets say you have a script called "test.sh", the contents would be:
echo "I am: $(whoami)"
echo "I am in: $(pwd)"
And a list of servers with 4 servers (2 fake, 2 are the same server, just to ensure it works)

To run it:

Quote

$ bash multi_exec.sh -s long_fake_servers.list -f test.sh

Heres the output from my system

Quote

$ bash multi_exec.sh -s long_fake_servers.list -f test.sh
/'\_/`\ (_ ) ( )_ _ ( _`\
| | _ _ | | | ,_)(_) ______ | (_(_) __ ___
| (_) |( ) ( ) | | | | | |(______)| _)_ (`\/') /'__`\ /'___)
| | | || (_) | | | | |_ | | | (_( ) > < ( ___/( (___
(_) (_)`\___/'(___)`\__)(_) (____/'(_/\_)`\____)`\____)
Helping make automation easier

Creator(1): Justin Hyland - Email: justin.hyland@gmail.com, Website: justinhyland.com
Creator(2): Kyle Corupe - Email: kyle.corupe@gmail.com, Website: kylecorupe.com
Version: 1.0 (BETA)
Created: 09/10/2011
Multi-Exec Website: linux-multi-exec - Uses RSA keys to execute scripts or commands on multiple servers in a list or passed via an argument - Google Project Hosting
_____________________________________________________________________________________________________
[1/4]
# Executing on: micro.aznetworkuptime.com
I am: root
I am in: /home/jhyland
[ OK ] Executed test.sh on micro.aznetworkuptime.com
_____________________________________________________________________________________________________
[2/4]
# Executing on: fake.doesntexist.com
lost connection
[FAILED] Unable to SCP test.sh to fake.doesntexist.com (Exit Status: 1)
_____________________________________________________________________________________________________
[3/4]
# Executing on: micro.aznetworkuptime.com
I am: root
I am in: /home/jhyland
[ OK ] Executed test.sh on micro.aznetworkuptime.com
_____________________________________________________________________________________________________
[4/4]
# Executing on: some.server.ad
lost connection
[FAILED] Unable to SCP test.sh to some.server.ad (Exit Status: 1)
_____________________________________________________________________________________________________
Summary
Total Servers: 4
Total Successes: 2
Total Failures: 2

Pretty cool huh? Of course thats with all the cool colorful options that show everything (except verbose from scp and ssh), if you cut it down to just get the basic output, you can get this:
(NOTE: To change the default settings, you can edit the default settings at the top of the file, so all the switches you see below dont NEED to be there, they are just there because I have the default settings set to the opposite)

Quote

$ bash multi_exec.sh -s long_fake_servers.list -f test.sh -l -U -C -b
_____________________________________________________________________________________________________
[1/4]
# Executing on: micro.aznetworkuptime.com
I am: root
I am in: /home/jhyland
[ OK ] Executed test.sh on micro.aznetworkuptime.com
_____________________________________________________________________________________________________
[2/4]
# Executing on: fake.doesntexist.com
lost connection
[FAILED] Unable to SCP test.sh to fake.doesntexist.com (Exit Status: 1)
_____________________________________________________________________________________________________
[3/4]
# Executing on: micro.aznetworkuptime.com
I am: root
I am in: /home/jhyland
[ OK ] Executed test.sh on micro.aznetworkuptime.com
_____________________________________________________________________________________________________
[4/4]
# Executing on: some.server.ad
lost connection
[FAILED] Unable to SCP test.sh to some.server.ad (Exit Status: 1)

Logging
Like I said, its got some pretty cool logging (the -l switch toggles it), here would be the output from the above runs

Failures

Quote

$ cat multi_exec-FAILED.log
# Failed execution log generated by multi_exec.sh
# Date: Thu Oct 13 00:01:08 MST 2011
# User: jhyland
# ScriptFile: test.sh
# Server File/List: micro.aznetworkuptime.com fake.doesntexist.com micro.aznetworkuptime.com some.server.ad
# Port: 22
# RSA Key: /home/jhyland/.ssh/id_rsa
# Verbose:
# Quiet: -q
# Sudoer: true
# SSH Config: /etc/ssh/ssh_config
# Interpreter: /bin/bash
# Color: false
# Destination Dir: ~/
# Arguments :
#
Thu Oct 13 00:01:19 MST 2011 | fake.doesntexist.com | Unable to SCP test.sh to fake.doesntexist.com | 1
Thu Oct 13 00:01:34 MST 2011 | some.server.ad | Unable to SCP test.sh to some.server.ad | 1
# Total Failed: 2/4
Successes

Quote

$ cat multi_exec-SUCCESS.log
# Successful execution log generated by multi_exec.sh
# Date: Thu Oct 13 00:01:08 MST 2011
# User: jhyland
# ScriptFile: test.sh
# Server File/List: micro.aznetworkuptime.com fake.doesntexist.com micro.aznetworkuptime.com some.server.ad
# Port: 22
# RSA Key: /home/jhyland/.ssh/id_rsa
# Verbose:
# Quiet: -q
# Sudoer: true
# SSH Config: /etc/ssh/ssh_config
# Interpreter: /bin/bash
# Color: false
# Destination Dir: ~/
# Arguments :
#
Thu Oct 13 00:01:17 MST 2011 | micro.aznetworkuptime.com
Thu Oct 13 00:01:24 MST 2011 | micro.aznetworkuptime.com
# Total Success: 2/4

With some bash magic from the real linux administrators, you can manipulate it quite a bit..

Quote

$ cat multi_exec-FAILED.log | egrep -v "^$" | egrep -v "^#" | awk -F\| '{print $2" failed, the reason was:"$3", the exit code was:"$4}'
fake.doesntexist.com failed, the reason was: Unable to SCP test.sh to fake.doesntexist.com , the exit code was: 1
some.server.ad failed, the reason was: Unable to SCP test.sh to some.server.ad , the exit code was: 1
Or lets say you want to only see failures with the error code 1

Quote

$ cat multi_exec-FAILED.log | egrep -v "^$" | egrep -v "^#" | awk -F\| '{if ($4 == " 1") print $0}'
Thu Oct 13 00:01:19 MST 2011 | fake.doesntexist.com | Unable to SCP test.sh to fake.doesntexist.com | 1
Thu Oct 13 00:01:34 MST 2011 | some.server.ad | Unable to SCP test.sh to some.server.ad | 1

Help
Heres the help menu, kinda long, but it shows a lot of helpful information

Quote

$ bash multi_exec.sh -hh
Arguments:
-s Server(s) (Can be a file with a list, a single server in a string, or multiple servers in quotes)
-f Script File (Relative path)
-S Do NOT execute the script on the remote server as a sudoer
-C Disable color output (useful for when used by a web interface, or something that can't translate bash color charactors)
-a Arguments to append to file when executing
-b Hides the Multi-Exec banner
-U Disables the summary display of the Multi-Exec after it concludes
-i Interpreter to execute script with, (defaults to /bin/bash)
-r Specify different RSA key to use (Default is /home/jhyland/.ssh/id_rsa)
-u Execute this as a different user, other than jhyland
-v Use verbose mode for debugging (-vv and -vvv for more intense levels)
-d Set the destination directory to SCP the file to and execute from, default is ~/
-p Specify a different port to use for SSH, default is 22
-c Specify a default configuration file, if -F is specified with a configuration file, /etc/ssh/ssh_config will be ignored
-q Do not use quiet mode, quiet mode causes most warning and diagnostic messages to be suppressed. Only fatal errors are displayed
-l Do not log failed executions to ./multi_exec-FAILED.log (exit code != 0), and successful executions to ./multi_exec-SUCCESS.log (exit code = 0)
-h Display this menu and exit, (-hh or -h -h will show a more intense help menu)
-V Show version and exit
-A Display the about page and exit

More Usage Examples:

Usage [1]: Execute bash script whoami.sh on example01.domain.tld
/bin/bash multi_exec.sh -s example01.domain.tld -f whoami.sh

Usage [2]: Execute perl script helloworld.pl on 2 servers
/bin/bash multi_exec.sh -s 'example01.domain.tld example02.domain.tld' -i /usr/bin/perl -f helloworld.pl

Usage [3]: Execute bash script ls.sh with the args '-alrth' on a list of servers without color
/bin/bash multi_exec.sh -s ./lists/allservers.txt -f ls.sh -a '-alrth' -C

Usage [4]: Execute python script whatsyp.py on example01.domain.tld using port 2222 for ssh, and a different user with a different RSA key
/bin/bash multi_exec.sh -s example01.domain.tld -i /usr/bin/python -f whatsup.py -p 2222 -r /home/foo/.ssh/id_rsa -u foo

Usage [5]: Execute bash script install.sh on a list of servers, using SSH increased verbosity (3 v's), and using an alternate SSH configuration file
/bin/bash multi_exec.sh -s ./lists/allservers.txt -f install.sh -vvv -C /home/foo/.ssh/ssh_config

Usage [6]: Execute bash script uninstall.sh on a list of servers, and log failures to ./multi_exec-FAILED.log and successes to ./multi_exec-SUCCESS.log
/bin/bash multi_exec.sh -s ./lists/allservers.txt -f uninstall.sh -vvv -C /home/foo/.ssh/ssh_config
NOTE: A 'failure' can be either failing to SCP the file to the server, execute it via SSH, or the actual return code of the executed file (Anything other than 0 = fail)

Usage [7]: Execute bash script reinstall.sh on a list of servers, use directory '/tmpdir/' as the directory to SCP the file to and execute from
/bin/bash multi_exec.sh -s ./lists/allservers.txt -f reinstall.sh -d /tmpdir/

Usage [8]: Execute bash script reboot.sh on a list of servers, without sudo permissions
/bin/bash multi_exec.sh -s ./lists/allservers.txt -f reinstall.sh -S

Let me know what you think! Didnt take too long to make it, but it makes my life easier
Checkout my new forum! http://adminreference.com/




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users