Last active
August 29, 2015 14:01
-
-
Save mmrwoods/164a4e3799dc4d9667e3 to your computer and use it in GitHub Desktop.
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 | |
# Find hosts matching pattern in ssh_config and run command on each. | |
# As pattern is an argument to egrep, but is also injected into a string | |
# which is evaluated, you can hack the input to do weird useful things, | |
# like pass options to egrep or pipe the output through another command. | |
usage() { | |
echo "Usage: $0 [pattern] [command]" | |
} | |
if [ "$1" == "" ] || [ "$2" == "" ]; then | |
usage | |
exit 1 | |
fi | |
hosts=`eval "egrep '^Host ' ~/.ssh/config | egrep $1" | awk '{print $NF}'` | |
for host in $hosts; do | |
printf "\e[32mConnecting to $host...\e[0m\n" | |
printf "\e[33m* executing \"$2\"\e[0m\n" | |
ssh -t $host "$2" | |
echo | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment