Skip to content

Instantly share code, notes, and snippets.

@progrium
Last active April 20, 2016 12:37
Show Gist options
  • Save progrium/8232241 to your computer and use it in GitHub Desktop.
Save progrium/8232241 to your computer and use it in GitHub Desktop.
bash script for executing a command via ssh in parallel on multiple servers with colored output
#!/bin/bash
#
# Usage:
# $ echo "host1 host2 host3" | ./sshmany uname -a
# $ cat myservers | ./sshmany echo Hello world
#
cmd="$@"
servers="$(cat)"
i=37
for server in $servers; do
echo "----> $server" | awk -v i=$i '{print "\033[" i "m" $0 "\033[39m"}'
i=$(expr $i - 1)
done
echo "====> $cmd"
i=37
for server in $servers; do
ssh $server $cmd | awk -v i=$i '{print " \033[" i "m" $0 "\033[39m"}' &
i=$(expr $i - 1)
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment