(Today’s post is by information security analyst Scott Pack!)
One of the worst problems I have with tabbed consoles is knowing exactly which console I’m working in. Sure, I can simply look at either the shell’s title, or the prompt, but I still inevitably will type a command into the wrong console. Normally this will result in something more akin to “file not found” because you catted the wrong file, rather than “Now running koan on all servers” because you did something in mcollective prod instead of test. While chatting up a guy at work about this problem we threw around changing the background color depending on which system you’re on.
This is one of those things that’s a little trickier than one might think at first blush. For instance, my first thought was to overload the ssh operator and make the changes client side. This works for me since I use cygwin as my ssh client and proxy through an aggregator (see this ServerFault answer about proxying</shill>). My coworker, however, uses puttycm which pretty much excludes any kind of local overloading. In the end, I opted to have the colorization occur on login. This made the process more portable, but it also meant that I have to make changes to my ~/.bashrc on every system.
In the end, I wanted this setup to work for both of our environments. So for my uses I wanted the background to get reset whenever I logged off a remote system, so that if I *did* manually ssh from one system to another, the background color should always be correct no matter what else I’ve been doing. So the colors needed to be set on login, which is easy, and also whenever ssh exits (I suppose I should also account for telnet/rsh but I really don’t want to). To that end, I decided to make the colorization a separate function so it could easily be reused.
It’s also worth noting that this was designed for a system running bash-3.x. If you’re in an entirely bash-4.x environment we can use an associative array, which works like a perl hash. Since I still deal with a lot of RHEL5 systems, which still uses bash-3.x, I had to code to it. To account for this I had to do some odd stuff with the array that basically amounts to magic.
Without further ado, below is everything you’ll need to add to your ~/.bashrc file to make this garbage work. With any luck, the comments are sufficient to explain.
function setcolor { # Set up the colormap using hex codes for the colors colormap=( "node1:#330000" "node2:#003300" "node3:#330033" "node4:#333300" "node5:#FF0000" "node6:#0000FF" ) # Generate my own short hostname, i.e. turn node1.example.com into node1 short=`echo ${HOSTNAME} | sed "s/\..*$//"` color="#000000" # Set default color to black # Iterate through the colormap looking for the hostname. Also, some bash magic. for host in ${colormap[@]}; do if [[ ${host%%:*} == ${short} ]]; then color=${host##*:} fi done # Wrap the color in the xterm escape sequences to set the background color echo -ne "33]11;${color}07" # Set the background color } # Only run the setcolor function if we are using xterm or xterm-color as our termtype if [ $TERM = "xterm" ]; then export TERM="xterm-color" setcolor elif [ $TERM = "xterm-color" ]; then setcolor fi # Replaces the shell title with the name of the host we are sshing to function ssh { echo -ne "33]0;${1}07" # Set the terminal title to the host we are sshing to /usr/bin/ssh $1 $2 $3 setcolor # Once ssh exits, reset the color back to what it should be }









