Why
Sometimes running commands across a range of servers is necessary. Yes, there is Ansible, Puppet and SaltStack etc., however in some cases this is overkill (and these usually require python, ruby or other languages installed.
The following shell script runs commands (via sudo if required) on a group of hosts in parallel. It is quite old and not very elegant but does the trick and hopefully helps somebody in the future. Please don't comment on syntax and bugs.
Configuration
The .run file in your home directory contains one line for user: and one line for pass:. Should this file not exist, the script will ask for the user and password interactively.
The servers file includes one line per host (or includes for additional host files) - or if the file does not exist it will be treated as single host name.
Usage
./run.sh server "uptime"
Script
#!/bin/bash
#
# About: Script to concurrently run commands on a list of servers (with or without sudo)
# Author: Lonestarr
# Last edit: 2012 05 24
# Version: 0.5
#
#####
# defaults
SLEEP=5
CONCURRENCY=0
RECURSIONS=6
# do not change below
user=`/usr/bin/id -un`
control="$HOME/.run"
file=""
command=""
MPIDS=""
PROCESSES=0
SERVERS=""
NUMSERVERS=0
RCOUNT=0
USESUDO=0
DEBUG=0
USECONFIRM=1
############################## ############################## ############################## ####
# FUNCTIONS
############################## ############################## ############################## ####
# running the actual command - via embedded expect script
run_on_server(){
SHORT=`echo $host | cut -d. -f1`
echo "$SHORT: running $command"
# /usr/bin/expect -d -c " # debug mode
/usr/bin/expect -c "
set timeout 10
# LOGIN to server
spawn ssh -q $user@$host
expect {
# PASSWORD LOGIN - send password
-re \"assword:\" {
send \"$pass\r\n\"
}
# save new ssh key - accept - send yes
-re \".*\(yes.*no\)\" {
send \"yes\r\n\"
exp_continue
}
}
sleep 2
# wait for prompt
expect \"*$user@$SHORT\"
# run commands requested
set timeout 200
send \"sudo $command\r\n\"
sleep 2
# sudo password - if necessary - otherwise exit ssh and close
expect {
-re \"assword for $user:\" {
send \"$pass\r\"
exp_continue
}
-re \"Password:|password for\" {
send \"$pass\r\"
exp_continue
}
-re \"$user@$SHORT\" {
send \"exit\r\"
}
}
" | egrep -v "^spawn ssh| sudo |RHN Satellite kickstart| password for |^Password:|^Last login:|^[[:space:]]*$" | sed -e "s/^/$SHORT: /g"
echo "$SHORT: completed."
}
# print usage and exit
print_usage(){
echo >&2 ""
echo >&2 "usage: $0 [-s delay] [-y] [-d] [-S] [-c concurrency] [-u user] <server name or server list file> <command>"
echo >&2 " -s 5 : sleep between running the commands on each server in seconds, the default is 5"
echo >&2 " -y : usually you will ask for confirmation prior to executing the commands - use this option to disable confirmation"
echo >&2 " -d : enable debug mode"
echo >&2 " -S : run command using sudo - alternatively can also be supplied as part of the command"
echo >&2 " -c 5 : use concurrency of 5, run a maximum of 5 servers in parallel. Use 0 to run on all servers in parallel"
echo >&2 " -u x : use a different user name than the script is running under, can also be supplied via .run config file"
echo >&2 ""
echo >&2 "host / server file: either a single server name or the name of a file containing host names or include <file> statements."
echo >&2 "command: command to run on the remote host. Needs to be quoted if it contains spaces or semicolons - also if multiple sudo"
echo >&2 "commands need to be executed, add sudo before each additional command"
echo >&2 ""
echo >&2 "Example: $0 -S defiant.myhome.com \"uname -a\""
echo >&2 ""
exit 3
}
# recurse host files
include_server_file()
{
# if not file, treat as server and add to list
if [ ! -e "$1" ]; then
SERVERS="$SERVERS
$1"
NUMSERVERS=$[$NUMSERVERS+1]
return
fi
# if it's a file, read hosts
while read host
do
if [[ $host == *include* ]]; then
FILE=`echo $host | cut -d" " -f2-`
include_server_file "`dirname $1`/$FILE"
else
SERVERS="$SERVERS
$host"
NUMSERVERS=$[$NUMSERVERS+1]
fi
done < "$1"
}
# get a list of running processes
getRunningJobs()
{
# get a list of all running processes
PROCS=`ps -e | awk '{ print $1 }' | grep -v PID`
RUNNING=0
# go through list of children and see if they are still running
for MPID in $MPIDS; do
IN=`echo $PROCS | grep -c " $MPID "`
if [ $IN == "1" ]; then
RUNNING=`expr $RUNNING + 1`
fi
done
return $RUNNING
}
############################## ############################## ############################## ####
# MAIN
############################## ############################## ############################## ####
# make sure we have at least 2 parameters
if [ $# -lt 2 ]; then
print_usage
fi
# read command line options
while [ $# -gt 0 ]
do
# only two parameters left, use as file and command
if [ $# -eq 2 ]; then
serverfile="$1"
command="$2"
shift; shift
else
# more options to read, overwrite defaults where necessary
case "$1" in
-s) SLEEP="$2"; shift;;
-u) user="$2"; shift;;
-c) CONCURRENCY="$2"; shift;;
-S) USESUDO=1;;
-d) DEBUG=1;;
-y) USECONFIRM=0;;
-h) print_usage;;
-*) print_usage;;
esac
shift
fi
done
# building server list / handle includes
include_server_file "$serverfile"
# ask for confirmation
if [ "x$USECONFIRM" == "x1" ]; then
if [ "x$USESUDO" == "x1" ]; then
echo "Are you sure you want to run: \"$command\" on these $NUMSERVERS servers (as root)?"
else
echo "Are you sure you want to run: \"$command\" on these $NUMSERVERS servers?"
fi
echo "$SERVERS"
echo
read -p "Please confirm (y/N): " confirmation
if [ "x$confirmation" != "xy" ]; then
echo "aborted."
exit 3
fi
fi
# if user / pass config file exists and has user / pass, get it from there
if [ -r "$control" ]; then
user=`grep user: $control | cut -d: -f2-`
pass=`grep pass: $control | cut -d: -f2-`
else
# ask for password
read -s -p "Please enter $user's password: " pass
echo
fi
# go through all servers in list
for host in $SERVERS; do
# run command in background
run_on_server &
MPIDS="$MPIDS $! "
PROCESSES=`expr $PROCESSES + 1`
sleep $SLEEP
# handle concurrency - and wait until some jobs have completed
if [ "x$CONCURRENCY" != "x0" ]; then
# wait until we can start more jobs again
while true; do
# get current number of running jobs
getRunningJobs
if [ "$?" -ge "$CONCURRENCY" ]; then
echo "Currency of $CONCURRENCY reached - must wait";
sleep 5
else
break
fi
done
fi
done
# waiting for all processes to finish
while : ; do
sleep 5
# check running processes - count number
getRunningJobs
RUNNING=$?
# are we finished yet?
if [ "x$RUNNING" == "x0" ]; then
echo "All $PROCESSES tasks completed."
exit
fi
# not yet, let the user know
echo "-> waiting for $RUNNING / $PROCESSES tasks to finish ... please be patient"
done
Comments
Post a Comment