Skip to content

Instantly share code, notes, and snippets.

@metalrufflez
Last active December 18, 2015 04:39
Show Gist options
  • Save metalrufflez/5726735 to your computer and use it in GitHub Desktop.
Save metalrufflez/5726735 to your computer and use it in GitHub Desktop.
A script that will grep your ~/.ssh/config for a certain expression, then creates a tmux session with a ssh session for each host matched. It's not pretty, there's lots of room for improvement, but it does the job.
#!/bin/bash
#
# tuxedo
# Creates a tmux session with hosts grepped from your ~/.ssh/config
#
# Created by: Caio Correa <[email protected]>
# Named by: @xfer
# Last Updated: 2013/06/06
#
# PROTIP: Add aliases for the most used sessions in your bash(rc|_profile)
# alias secrprojects="tuxedo -o staging -i xen,old PROJECTX projx projy projz
#
function usage() {
echo "${0##*/} -dh [-c config/file] [-i ignore1,ignoreN] [-o only1,onlyN] <SESSION> <prefix1 [prefixN]>"
}
function print_help(){
usage
echo
echo "REQUIRED PARAMETERS:"
echo -e "<SESSION>\t\tSession name."
echo -e "<prefix1 [prefixN]>\tPrefixes that will be matched to the .ssh/config."
echo
echo "OPTIONS:"
echo -e " -c\t\tConfig file. Default: ~/.ssh/config."
echo -e " -d\t\tDry Run. Prints the parameters and matches, then exits."
echo -e " -h\t\tPrints this help."
echo -e " -i\t\tList of ignored statements separated by commas."
echo -e " -o\t\tList of required statements separated by commas."
echo
}
function error() {
echo $1
exit 1
}
function dry_run() {
echo sshconfig = $sshconfig
echo ignores = $ignores
echo only = $only
echo session = $session
echo prefixes = $prefixes
echo hosts = $hosts
}
sshconfig="$HOME/.ssh/config"
# Check if tmux is installed
hash tmux 2> /dev/null || error "tmux is not installed"
# Check if there is at least one parameter
[[ "$#" -eq 0 ]] && usage
# Gets options
while getopts ":c:dhi:o:" option; do
case $option in
c) sshconfig="$OPTARG";;
d) dry=true;;
h) print_help
exit 2
;;
i) ignores=$(echo "($OPTARG)" | tr "," "|");;
o) only=$(echo "($OPTARG)" | tr "," "|");;
*) usage
echo "Invalid Option"
exit 1
;;
esac
done
shift $(($OPTIND - 1))
# Check if $sshconfig exists
[[ ! -a $sshconfig ]] && error "The file $sshconfig does not exist"
# Check if you have the session name and at least one prefix
[[ "$#" -lt 2 ]] && error "Please provide the Session name and at least one prefix"
# The first parameter should always be the session name
session=$1
shift
# Parses the remaining variables to a grepable format
prefixes=$(echo "($*)" | tr " " "|")
hosts=$(egrep -i --color=never "host $prefixes" $sshconfig | awk '{ print $2 }' | sort)
# If there are no matching host, exits
[[ -z "$hosts" ]] && error "There was no match in your hosts for the provided parameters"
# If there is an ignore or only clause, apply it now
[[ ! -z $ignores ]] && hosts=$(echo "$hosts" | egrep -v "$ignores")
[[ ! -z $only ]] && hosts=$(echo "$hosts" | egrep "$only")
# if dry run is set, only prints the variables
if ${dry:-false}; then
dry_run
exit 2
fi
# Change Tab Name (
echo -e "\033]0;$session\007"
# If there isn't already a tmux session with the $session name, creates one
if ! tmux ls | egrep -q "$session:"; then
tmux new-session -d -s $session || exit 1
# For each item, creates a new window and ssh the host
for host in $hosts; do
tmux new-window -t $session -n "$host" "ssh $host"
done
# Housecleaning
tmux kill-window -t $session:0
tmux select-window -t $session:1
fi
# Attach the session
tmux attach-session -t $session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment