Created
February 25, 2016 10:28
-
-
Save gjask/a2a71d621fc48cd677b7 to your computer and use it in GitHub Desktop.
Naive tool for distributed execution of commands and shell scripts on remote machines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# author: jask | |
# WARNING: this script is intended to use only for those who have deep understanding how it works, so enjoy the fun with it ;) | |
interpret='bash' | |
#ssh_args='-i .ssh/atollon' | |
#user='jask' | |
usage(){ | |
cat <<EOF | |
Run local bash script on remote hosts. | |
Warning: Uses parallel proccesses without good management! | |
Usage: $0 [OPTIONS] [hosts] | |
Options: | |
-s Will run in sudo | |
-f path Path to the script. If note set, standard input will be used | |
-a args args string will be passed as list of arguments to the script (use with -f) | |
-c command Instead of script file you can set bash oneliner | |
-x Run bash in debug mode (bash -x) | |
-h path Path to the file with list of remote hosts | |
-q Silences outputs from hosts, with return code zero | |
Examples: | |
./remote-script.sh -s -f create_user.sh -a "jura '\$( cat jura.pub )'" -h lagoons | |
EOF | |
} | |
while [ -n "$1" ]; do | |
case "$1" in | |
-s) sudo='sudo';; | |
-f) file=$2; shift;; | |
-c) com="$com $2;"; shift;; | |
-x) mods="$mods -x";; | |
-h) hosts="$hosts $( cat $2 )"; shift;; | |
-i) interpret="$2"; shift;; | |
-m) mods="$mods $2"; shift;; | |
-A) ssh_args="$ssh_args $2"; shift;; | |
-a) args="$args $2"; shift;; | |
-u) user=$2; shift;; | |
-e) vars="$vars $2"; shift;; | |
-q) quiet='yes';; | |
-t) clock='time';; | |
*) hosts="$hosts $1";; | |
esac | |
shift | |
done | |
[ -z "$hosts" ] && usage >&2 && exit 1 | |
if [ -z "$file" ]; then | |
file=$( mktemp ) | |
tmp=1 | |
if [ -n "$com" ]; then | |
echo "$com" > $file | |
else | |
cat > $file | |
fi | |
fi | |
[ -n "$user" ] && user="$user@" | |
tmpd=$( mktemp -d ) | |
touch $tmpd/blank | |
proceed(){ | |
log=$tmpd/$host | |
echo "==== $host ====" > $log | |
cat $file | ssh $ssh_args ${user}${host} "$clock $sudo $vars $interpret $mods -s $args 2>&1; echo \$?" >> $log | |
[ "$quiet" == 'yes' -a "$( tail -1 $log )" -eq 0 ] && rm -f $log && return | |
echo >> $log | |
} | |
for host in $hosts; do | |
proceed & | |
done | |
wait | |
cat $tmpd/* | |
[ -n "$tmp" ] && rm -f $file | |
rm -rf $tmpd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment