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


Click here to return to the 'SSH_HOST: New Version' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
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 | # ]