Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmrwoods/164a4e3799dc4d9667e3 to your computer and use it in GitHub Desktop.
Save mmrwoods/164a4e3799dc4d9667e3 to your computer and use it in GitHub Desktop.
#!/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