Hey kids. Do you ever find yourself working at your terminal, with the dull glow barely revealing the letters on your keyboard, attempting to do the same thing to all the regions in your company? Your friends have, in quiet whispers, mentioned pssh, screen, and tmux as the best trip you’ll ever have. As a smart developer you cautiously dabbled in them at first. Maybe you found what you were looking for, but then in the thick of things you found yourself banging your head on that same keyboard. The trip just wasn’t worth it.

What are we talking about again?

Automating the tmux splitting

This is a simple bash script to get tmux to split into N panes in a new window. It assumes you’re already in a tmux session when you do this. A lot of scripts do this but they assume you’re not in a tmux session and typically create a new session.

I practically live in tmux and can’t afford that huge session addition to my house.

Why you do dis?

During my work I often need to connect to a database in different regions and run the same query on them. While there are many ways to do this with just mysql, I like having the option to dabble quickly with a region specifically. Normally my workflow goes like this:

  • Create a new tmux window
  • Split it into N panes
  • Synchronize the panes and type my ssh connection command
  • De-sync the panes and change each hostname manually
  • Synchronize the panes again and hit enter, then password
  • Profit!

While that process is definitely not a long one, it’s annoying to do often and borderline anxiety-attack-inducing slow during things on fire moments.

I’d like to type something like this:

make_my_regional_windows ssh roaet@super-db.$region.roaet.com

This does it:

Cooode

#!/bin/bash

if [[ "$#" -lt 1 ]]; then
  echo "Usage: $0 <name> [command]"
  exit 1
fi

cmd_string=""
if [[ "$#" -eq 2 ]]; then
  cmd_string=$2
fi

print_cmd=echo

if hash figlet 2>/dev/null; then
  print_cmd=figlet
fi

regions=( "ord" "dfw" "iad" "lon" "syd" "hkg" )

tmux new-window -a -n "$1"

for ((i=1; i<${#regions[*]}; i++));
do
  tmux split-window -h
done

tmux select-layout tiled

for ((i=0; i<${#regions[*]}; i++));
do
  tmux select-pane -t $i
  tmux send-keys 'export region='${regions[i]}'; '$print_cmd' $region' C-m
done

tmux setw synchronize-panes
tmux select-pane -t 0

if [[ -n "$cmd_string" ]]; then
  tmux send-keys -l "$cmd_string"
fi

Usage:

You can name the file whatever you want, but for this example I shall name it region_split.sh, put it somewhere my $PATH can find it, and alias it in bash to regionsplit.

regionsplit fk_errors "ssh roaet@db.$region.roaet.com"

This will:

  • open a new tmux window named fk_errors (I name it what I’m working on)
  • echo out the $region variable as defined in the array in the script
  • put the command ssh roaet@db.$region.roaet.com in the command-line

Now all I have to do is hit enter to start the ssh sessions everywhere.

Mm-yeah.

Asterisks n’ stuff

  • Whatever command you put in there needs to be surrounded by quotes
  • Definitely change the region array to meet your needs
  • People who don’t know what an fk is will just think you’re being vulgar :/