Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A shell script to set Terminal window colors per SSH host UNIX
I wanted Terminal's background color to change based on the SSH host I access, and then change back when I log out. This hint describes how to programmatically control the color and opacity of newly created terminal windows, and is part of the solution. Unfortunately, it works by setting the properties of the front window. When the Terminal is used in focus follows mouse, the active window is not always the front window, and this method sometimes changes colors in the wrong window.

To solve this, I wrote a modified script (4KB binary download -- it's binary to maintain the BEL and Escape characters encoded in the script) that first sets a unique window title, and then selects the appropriate window based on that title. With one script, you can manage as many different SSH host/color combinations you'd like. Simply add custom colors to the case statement where indicated, and link to the script using the name of the new host you'd like. Example:
% cd bin
% ln -s ssh_host foobar
% rehash    ## for tcsh shell [or]
% hash -r   ## for bash shell
% foobar
To keep host names short, you can either add an /etc/hosts alias, or link with the full hostname (e.g. ln -s ssh_host foobar.baz.com) and create a shell alias for it. If you set your own window title with a precmd alias or equivalent, you might want to remove the last echo (which clears the title).
    •    
  • Currently 3.20 / 5
  You rated: 5 / 5 (5 votes cast)
 
[20,659 views]  

A shell script to set Terminal window colors per SSH host | 15 comments | Create New Account
Click here to return to the 'A shell script to set Terminal window colors per SSH host' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
/etc/hosts for ssh aliases?
Authored by: rootpoot on Aug 02, '05 11:53:58AM

Why not use ~/.ssh/config?


Host most
HostName host.with.the.most


[ Reply to This | # ]
.term files
Authored by: pauljlucas on Aug 02, '05 12:26:12PM

You cal also just create a window in Terminal, customize it however you like, set the command to exevute to be "ssh somehost", then save the .term file. Now you have a double-clickable Terminal document that will connect to somehost with all the window customizations you set.

---
- Paul



[ Reply to This | # ]
A shell script to set Terminal window colors per SSH host
Authored by: fungus on Aug 02, '05 12:54:17PM
I have another idea to use this script that would be more transparent to the user.
  • Change the host variable to use the argument instead of the script name.
  • In your shell, alias ssh to ssh_host.
  • Now, calling ssh hostname will set the color and call ssh.
With a little work you could also pass other arguments to ssh.

[ Reply to This | # ]
A shell script to set Terminal window colors per SSH host
Authored by: capacity on Aug 02, '05 05:29:41PM
fungus, can you please explain in a little more detail? I'm new to this shell script stuff, and I'm not exactly sure how to change the host=${1:-${0##*/}} line to do what you're talking about (I think that's what you're referring to). Also, I have ~/bin/ in my PATH, but would I have to chmod +x ssh_host.sh? thanks.

[ Reply to This | # ]
A shell script to set Terminal window colors per SSH host
Authored by: jdsmith on Aug 03, '05 12:43:14PM
The script already works that way. It can either take a single argument (the ssh host name), or use the base name of the linked command name. To get your functionality, simply:
% alias ssh ssh_host
% ssh hostname

By the way, the ".sh" in the name of the downloaded file is just there to ensure the special characters are downloaded correctly. It can safely be stripped, so the script is just called ssh_host.

For the interested, host=${1:-${0##*/}} is just shell magic for "use the first argument, unless it isn't set, in which case use the command name, stripped of any leading path". JD

[ Reply to This | # ]

A shell script to set Terminal window colors per SSH host
Authored by: the_shrubber on Aug 02, '05 01:50:09PM
See also this hint - don't know if it works on Tiger, though. The link on this hint is broken... You'll have to go to the download page instead.

[ Reply to This | # ]
A shell script to set Terminal window colors per SSH host
Authored by: kenahoo on Aug 02, '05 05:41:20PM

Rather than encoding the Escape and Bel characters right in the script, you can use

   echo -n -e "\e]0;newWindowName\a"

. This will confuse people less than trying to remember ASCII mappings.



[ Reply to This | # ]
A shell script to set Terminal window colors per SSH host
Authored by: jdsmith on Aug 03, '05 01:45:57PM
Really? My OSX-native tcsh built-in echo does not understand "-e", or I would have gone that route:
% echo -n -e "\e]0;newWindowName\a"
-e \e]0;newWindowName\a

Aha, but I see the default bash built-in echo does permit this, so I should modify the script. Since I use tcsh as my default shell, I wasn't aware of this (a good tip on its own). I'll submit a revision.

[ Reply to This | # ]

printf
Authored by: sjk on Aug 24, '05 08:55:57PM

Or use the printf command instead of echo.



[ Reply to This | # ]
A shell script to set Terminal window colors per SSH host
Authored by: andriven on Aug 03, '05 01:15:55AM

Any thoughts on modifying to just give a random color for each ssh window/sessions? (out of 10-20 eye-safe colors)

I ssh into a lot of boxes.....so setting up a custom color per box isn't worth it but having different colors (even if randomly) would be very nice (hopefully two ssh sesssion to the same box would get the same color even if in different Terminal windows).



[ Reply to This | # ]
A shell script to set Terminal window colors per SSH host
Authored by: bendybendy on Aug 03, '05 12:24:58PM

I got some thoughts (but no code!)

1- choose the colors based on the host name- say the first 3 consonants.
2- use color values from the top or bottom of the 0-65535 range, to keep the colors subtle and eye safe.

That way, you'd get the same color whenever you went to the same host.



[ Reply to This | # ]
SSH_HOST: New Version
Authored by: jdsmith on Aug 03, '05 01:59:50PM
I've updated the script so that it no longer uses binary special characters, but echo -e instead (thanks kenahoo). Also, it now chooses a default background color based on the first three characters of the hostname, if you haven't set up a match by default. It maps a-z to 0-65535 for red, green, and blue. This is not guaranteed to produce eye pleasing colors (e.g. ssh_host zzz would be non-ideal with white text), so do setup explicit matches in problem cases. You could probably also set the text color based on the absolute luminance of the bg color, but I didn't implement that.

#!/bin/sh
#
# ssh_host - JD Smith, Copyright (2005)
#
# Manage OSX terminal colors for various ssh hosts.  Works even with
# focus-follows-mouse, when the focused terminal is not the foremost.
# 
# Can be called as:
#
#   ssh_host hostname
#
# or, by making a link to this script:
#
#   cd ~/bin/
#   ln -s ssh_host hostname
#
# and then using the script link "hostname" without arguments.
#
# Configure host-based color below.  Unconfigured hosts will have a
# default color chosen based on the first three characters of their
# hostname.
#

host=${1:-${0##*/}}

########################################################################
# User editable section: Configure custom host colors here, adding new
# host cases as necessary
#
# Color Format: "R, G, B, A" for red, green, blue, and opacity (0-65535)
#
# Example line to add:
#   foobar.baz.com*) host_color="20000, 20000, 10000, 65535";;
#

# The default opacity value if not specified (0-65535)
default_opacity=61000

# The default color which will be used when not connected to any host.
default_color="0, 0, 0, $default_opacity"

case "$host" in
### Add matching host cases here:
    turtle*) host_color="5000, 20000, 5000, 63000";;
########################################################################

    *)  # Compute default color based on first three letters of hostname
	declare -a cols
	cols=($(echo -ne $host | tr 'A-Z' 'a-z' | tr -cd 'a-z' | od -t d1 | \
	    head -n1 | cut -c10-22));
	r=$(expr \( ${cols[0]:-122} - 97 \) \* 13107 / 5 );
	g=$(expr \( ${cols[1]:-122} - 97 \) \* 13107 / 5 );
	b=$(expr \( ${cols[2]:-122} - 97 \) \* 13107 / 5 );
	host_color="$r, $g, $b, $default_opacity";
	;;
esac

window_name="${host}_SSH_$$"

trap cleanup 1 2 3 6

function cleanup() {
    set_color "$default_color"
    echo -n -e "\e]0;\a"
}

function set_color() {
    echo -n -e "\e]0;${window_name}\a"
    osascript -e 'tell application "Terminal" to tell (first window whose name contains "'$window_name'") to set background color to {'"$1"'}'
}

set_color "$host_color"
ssh -X $host
cleanup


[ Reply to This | # ]
SSH_HOST: New Version
Authored by: bendybendy on Aug 10, '05 01:22:04PM
Sweeeet! I often ssh in as another username, so to strip out the username from the "ssh user@hostname" evocation, I changed your color-fetching line to

cols=($(echo -ne ${host/[a-z]*@//} | tr 'A-Z' 'a-z' | tr -cd 'a-z' | od -t d1 | \
            head -n1 | cut -c10-22));


[ Reply to This | # ]
SSH_HOST: New Version
Authored by: bendybendy on Aug 10, '05 01:29:18PM

and I should add, if you ssh into servers named "www" you should put specifc colors into the case statement, lest your screen go to a very pale grey.



[ Reply to This | # ]
SSH_HOST: New Version
Authored by: mingking on Aug 23, '05 06:01:16PM
Very nice indeed. But of course I needed some tweaks :-) - since my terminal windows start out with custom colors, I wanted to save the current settings and then restore them on cleanup.

So far I added the following to the set_color() function:

orig_color=`osascript -e 'tell application "Terminal" to tell (first window whose name contains "'$window_name'") to get background color'`

and in the cleanup function I changed the resetting of the color to:

set_color "$orig_color"

Next I'll probably add support for setting the foreground text color. And then probably the hightlight color, cursor color etc, since those need to all track each other. Should be straightforward now given this template. This is something I've wanted/needed for a while. I kept making mistakes by entering commands to the wrong machine - but no more. Thanks for the kick start!

Then, how 'bout a version that works in an xterm? I often use an xterm so that I can launch remote X apps. Unfortunately AppleScript won't be available in that case. Any ideas? Ideally it would all be in one script...

[ Reply to This | # ]