Skip to content

Instantly share code, notes, and snippets.

@dialtone
Created October 31, 2013 03:42

Revisions

  1. dialtone created this gist Oct 31, 2013.
    53 changes: 53 additions & 0 deletions connect.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/bin/bash
    SSH_KEY=
    USERNAME=

    usage() { echo "Usage: $0 -s <tmux_session_name> [-k <my_ssh_key.pem>] [-u <username>] <host> <host1> ..." 1>&2; exit 1; }

    while getopts "s:k:u:" option;
    do
    case "${option}" in
    s) SESSION_NAME=${OPTARG};;
    k) SSH_KEY=${OPTARG};;
    u) USERNAME=${OPTARG};;
    *) usage;;
    esac
    done

    if [[ "$SESSION_NAME" == "" ]]; then
    usage
    fi

    if [[ "$SSH_KEY" == "" ]]; then
    SSH_KEY_PART=
    else
    SSH_KEY_PART="-i $SSH_KEY"
    fi

    if [[ "$USERNAME" == "" ]]; then
    USERNAME_PART=
    else
    USERNAME_PART="$USERNAME@"
    fi

    shift $((OPTIND-1))
    HOSTS=($@)



    tmux new -d -s $SESSION_NAME
    for index in ${!HOSTS[*]}
    do
    if [[ "$index" == "0" ]]; then
    continue
    fi
    tmux split-window -t $SESSION_NAME.0 -h
    done
    tmux select-layout -t $SESSION_NAME even-horizontal
    for index in ${!HOSTS[*]}
    do
    host=${HOSTS[$index]}
    tmux send -l -t $SESSION_NAME.$index "ssh $SSH_KEY_PART $USERNAME_PART$host"
    done
    tmux attach -t $SESSION_NAME