Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an easy way to have terminal use a different color based on ssh host name?

Using putty in windows, you can save sessions that connect to a certain host and use a certain text color...this was super useful for me since I work with a bunch of remote hosts and I'm wondering if there is (there must be) a way to get Terminal (in Snow Leopard) to emulate this behavior.

I'm wondering how I would 1. Save a connection (e.g. [email protected]) and have that connection always open with a certain text color (e.g. #00ff00) 2. Ideally, have any terminal window detect what host it was in and change its color accordingly. So if I was in my regular Terminal environment and issued a successful ssh [email protected], it would automatically change the text color of that terminal window (or tab) to #00ff00

Let me know, thanks!

like image 568
Neil Sarkar Avatar asked Dec 15 '09 06:12

Neil Sarkar


2 Answers

OK, if you insist on invoking ssh from the command line, here's something that should do the trick: write a shell script and save it somewhere as colorssh.sh. When it runs, it looks at its arguments for a matching host and sets the active terminal window's colors appropriately. Then it invokes the real ssh, passing along those arguments. When ssh returns execution to the script, it sets the colors back to normal.

Since you probably want to keep typing ssh instead of colorssh.sh, you can set an alias in your .profile.

As for the script itself? Here is teh codez:

#!/bin/bash

function setTerminalColors {
    osascript \
        -e "tell application \"Terminal\"" \
        -e "tell selected tab of front window" \
        -e "set normal text color to $1" \
        -e "set background color to $2" \
        -e "end tell" \
        -e "end tell"
}

for ARG in $*
do
    case "$ARG" in
        host.example.com)
        [email protected])
            setTerminalColors "{0,65535,65535}" "{65535,0,0}"
            ;;
        [email protected])
            setTerminalColors "{65535,65535,0}" "{0,65535,0}"
            ;;
    esac
done

ssh $*

# back to normal
setTerminalColors "{0,0,0}" "{65535,65535,65535}" 

You'll have to edit the script to add new host/color combinations.

Note that colors must be specified as an RGB triplet of integers in the range 0-65535. I know, weird, right?

Technically, the AppleScript portion changing deprecated properties. You're supposed to change a window's colors via its "settings set" property, but I suspect that would change all windows using that settings set, not just the current one.

Also, this script assumes that black on white is your "normal" setting. If that's not the case you could change the script to save the current values before running or use the colors from the default settings set.

like image 154
benzado Avatar answered Nov 15 '22 05:11

benzado


In terminal you can define profiles with different window background color, opacity, etc. Also in profiles you can specify a startup command. You could set up a different profile for each host you use with a startup command of "ssh me@thathost", but this would only work for new windows. Profiles are easy to get to via Shell -> New Window.

like image 25
spieden Avatar answered Nov 15 '22 07:11

spieden