?>

Simplifying host identification with rainbow terminals

In my daily work, I end up having to ssh into a variety of hosts. Keeping track of which terminal is on which host can become challenging when I have 3 or 4 terminals all at a mysql prompt, or tailing log files. A co-worker of mine came up with a pretty clever solution that I wanted to share. The clever solution involves some bash, and Applescript (as we’re working off of OSX). By aliasing ssh to a custom script, we can modify the terminal background colour. Having colour-coded terminals makes it really easy to figure out which host you’re working on.

The bash script that powers the automatic colour changing behaviour looks like:

Show Plain Text
  1. #!/bin/bash
  2. #
  3. if [ "$2" ]; then
  4.     exec ssh $*
  5. fi
  6.  
  7. hostspec=$1
  8. host=$(echo $hostspec | sed -e 's/^.*@//' -e 's/\..*$//')
  9.  
  10. # The default color which is pale yellow (solarized)
  11. # rgb(253,246,227) * 257 to get 16bit color values.
  12. default_color="65021, 63222, 58339"
  13.  
  14. case "$host" in
  15. #
  16. # Add specific host cases here:
  17. #
  18.     prod) host_color="12000, 2000, 12000";;
  19.     staging)  host_color="5000, 5000, 11808";;
  20.     devimage) host_color="8000, 8000, 12000";;
  21. #
  22. #######
  23.     *)  # Compute default color based on first three letters of hostname
  24.     declare -a cols
  25.     host_color=$(perl -e '
  26.          $DARKNESS = 4;
  27.          $host = $ARGV[0];
  28.          while ($host =~ /(.)/g) { $seed += ord($1) };
  29.          srand($seed);
  30.          for (1..3) {
  31.              $c = int(rand() * 65535);
  32.              if ($c > (65535/$DARKNESS)) {
  33.                  $c = $c / $DARKNESS
  34.              }
  35.              push @colors, $c;
  36.          };
  37.          print join (", ", @colors);
  38.      ' $host)
  39.     ;;
  40. esac
  41.  
  42. window_name="${host}_SSH_$$"
  43.  
  44. function set_color() {
  45.     case "$TERM_PROGRAM" in
  46.     iTerm.app)
  47.         osascript <<EOF
  48.         tell application "iTerm2"
  49.             tell current window
  50.                 tell current session
  51.                     set background color to {$1}
  52.                 end tell
  53.             end tell
  54.         end tell
  55. EOF
  56.         ;;
  57.     esac
  58. }
  59.  
  60. set_color "$host_color"
  61. LOCAL_TERMCOLOR="$host_color" ssh -o SendEnv=LOCAL_TERMCOLOR $hostspec
  62. set_color "$default_color"

I install this script as part of my dotfiles but you could put it in /usr/local/bin/colorssh. Then in your bash profile, you can include alias ssh=/usr/local/bin/colorssh. After reloading your bash environment, you should see your terminal background change colours each time you ssh into a machine. While the script ensures each host has the same colour all the time, if you’re not happy with a colour, you can always override it with a specific case. For example, I make my ‘prod’ hostname a lovely purple with:

Show Plain Text
  1. prod) host_color="12000, 2000, 12000";;

If the colour values look a bit alien, its because iTerm2 and Terminal.app want 16bit colour values. An easy way to get those values is to take your standard RGB values and multiply each value by 257.

Comments

You really make it seem so easy along with your presentation but
I find this matter to be really something that I think
I’d never understand. It kind of feels too complex and very extensive for me.
I am having a look ahead in your next put up, I’ll attempt to get the hold of it!

TV Nitro 2 days, 9 hours ago

Have your say: