Set terminal background color based on system name

In small to medium size companies where one has to wear multiple hats and work on different systems at the same time it is critical to take help from the automated systems as much as any other tool to ensure and be aware of the context/system that one is on.   It is not uncommon to have multiple terminal windows open and connected to development, QA, stage and production environments at the same time.

As one switches from one system to another any mistake can have ugly impact especially when dealing with production issues under stress.  A simple “rm” command or “sql drop table on fact” from Vertica vsql, say, can create havoc.   I understand that you can put many checks in place including have appropriate access, aliases (rm=’rm -i’, for example), set prompts (PS1, PS2..) with hostname, color, etc.  But it is also possible to override alias or unalias to delete bunch of files with run “rm -f”.  
With many checks in place, one thing that I always prefer is having the background of the terminal color set based on hostname or system type – dev, production, etc.  On Mac I use following script and name it something like ssh2 and set the $PATH (bash) and have alias for many of these systems like

alias sshProdBox1=’ssh2 shiva@productionBox1.anywhere.com’

Script uses bash shell and AppleScript (http://en.wikipedia.org/wiki/AppleScript and https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptX/AppleScriptX.html and http://en.wikibooks.org/wiki/AppleScript_Programming ) on Mac.

On connecting to remote box background color is appropriately set – red  for production, light blue for QA,  gray for Dev, etc.  and on disconnecting terminal is reverted back to white fonts with black background. 

#!/bin/bash                                                                                                                                              
# Local window font is white on black background.  On exiting from any ssh reset the terminal window to this config (white fonts on black screen).

HOSTNAME=`echo $@ | sed s/.*@//`
# echo $HOSTNAME, $@
if [ ${HOSTNAME} ]; then
    echo “ssh to $HOSTNAME”
else
    echo “Missing ssh host. Exiting.”
    exit 1
fi

set_bg_color () {
    # color values are in ‘{R, G, B, A}’ format, all 16-bit unsigned integers (0-65535)
    osascript -e “tell application \”Terminal\” to set background color of window 1 to $1″
}

set_font_color () {
    osascript -e “tell application \”Terminal\” ” \
              -e    “tell selected tab of front window” \
              -e        “set normal text color to $1” \
              -e    “end tell” \
              -e “end tell”
}

# On exit from connecting revert back to local setting – white fonts on black background
on_exit () {
    set_bg_color   “{0, 0, 0, 50000}”                            # Black bg
    set_font_color “{65000, 65000, 65000, 50000}”   # White font
}
trap on_exit EXIT

# Main
case $HOSTNAME in

    # My dev with white fonts on dark gray)
        set_bg_color    “{15000, 15000, 15000, 50000}”
        ;;

….
….
esac
Remote dev box:
Remote production box:
Remote qa box:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s