Thursday, April 8, 2010

How to change your OSX terminal color for ssh sessions

Sometimes I have lots of terminal windows open for ssh sessions to multiple machines. It can all be a bit confusing even if I use the remote machine name in the shell prompt. So I got terminal colorization working for me with help from here and here.
There is a default color for all screens that are using ssh and you can also customize the colors for different remote machines. When you end the ssh session, the terminal returns to black on white.

This is for OS X because it uses the osascript command to change the colors but the rest of the shell script should be just fine for Unix.


$ cat /usr/local/bin/ssh
#!/bin/sh

# Color the OSX Terminal app according the hostname used in ssh
# Assumes you log in with a simple "ssh myuserid@remotehost"
# Place in a directory such as /usr/local/bin that is before the real ssh location

# The value of whatever was after the @ in the command
HOSTNAME=`echo $@ | sed s/.*@//`

set_bg () {
osascript -e "tell application \"Terminal\" to set background color of window 1 to $1"
}

set_text () {
osascript -e "tell application \"Terminal\" to set normal text color of window 1 to $1"
}

# Return to black text on white background
on_exit () {
# echo exited
set_bg "{65535,65535,65535}"
set_text "{0, 0, 0}"
}
trap on_exit EXIT

# My color names
# green 10000,40000,40000
# blue 10000,20000,60000
# orange 60000,40000,00000
# red 60000,00000,00000
# yellow 60000,60000,40000

case $HOSTNAME in
fisheye) set_bg "{60000,60000,40000}" ; set_text "{0,0,0}" ;;
jira|jira2) set_bg "{10000,40000,40000}" ; set_text "{0,0,0}" ;;
# unused colors follow
# blue) set_bg "{10000,20000,60000}" ; set_text "{65535,65535,65535}" ;;
# orange) set_bg "{60000,40000,00000}" ; set_text "{0,0,0}" ;;
# red) set_bg "{60000,00000,00000}" ; set_text "{0,0,0}" ;;
# yellow) set_bg "{60000,60000,40000}" ; set_text "{0,0,0}" ;;
# gray) set_bg "{60000,57000,55000}" ; set_text "{0,0,0}" ;;
*) set_bg "{60000,57000,55000}" ; set_text "{0,0,0}" ;;
esac

/usr/bin/ssh "$@"

No comments: