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

Color Terminal screens and ssh revisited yet again UNIX
After at least five different hints on the matter, you may be getting sick of hearing about coloring your Terminal when you run ssh.

Well, relax! This hint actually reduces the number of total hints. JD Smith and I have collaborated to produce a joint version of our previous hints, ssh_host (link #2 above) and colorssh (#4 above). The new script is called colorwrap (2KB download) and includes the following features:
  • Simplicity (from ssh_host)
  • No more wrong windows getting colored (from ssh_host)
  • Detection of previous color settings (from colorssh)
  • Ability to wrap arbitrary commands (from colorssh)
  • Handling of both foreground and background colors (brand new)
To use it, simply prefix your command with colorwrap, like this:
colorwrap ssh -X bubba@some.machine.com
I personally like to alias my ssh command to colorwrap ssh. You can also edit the script to customise the colors that get set. One handy trick is to use the GUI to the set the colors to taste, and then run the following command to find the relevant codes:
colorwrap getcolor
Use those codes when editing the file to set your own custom colors.

[robg adds: I tested the script, and it worked fine, though I chose to remove the .sh from the script's name ... and remember to use chmod a+x colorwrap to make it executable.]
    •    
  • Currently 2.20 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[20,838 views]  

Color Terminal screens and ssh revisited yet again | 10 comments | Create New Account
Click here to return to the 'Color Terminal screens and ssh revisited yet again' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Color Terminal screens and ssh revisited yet again
Authored by: pauljlucas on Sep 23, '05 12:21:08PM

I still don't get why people like to make simple things complicated. Just create a Terminal window as you want it, color and all, and save its .term file. You change the Shell plist item to be "ssh whatever" and it works.

Unless you're ssh'ing to lots of different hosts all the time, the above is good enough.

---

- Paul



[ Reply to This | # ]
Color Terminal screens and ssh revisited yet again
Authored by: tomholio on Sep 23, '05 03:27:29PM
Unless you're ssh'ing to lots of different hosts all the time, the above is good enough.
uhm, that's pretty much the whole point! thanks to the two authors for collaborating and wrapping everything up!

[ Reply to This | # ]
Color Terminal screens and ssh revisited yet again
Authored by: the_shrubber on Sep 23, '05 07:27:11PM

That's one good way to do things (especially since it lets you set all the colours, unlike our script). But for me, invoking a special purpose Terminal for a specific subtask doesn't come very naturally. My perhaps suboptimal tendency whenever I want to ssh somewhere is to pick up whatever terminal I have nearby and just use that. This script is useful for dealing with this sloppy behaviour.

Also, one thing which seems harder with the save-a-Terminal method is using ssh-agent. I tend to set up an ssh-agent, paste the details into a Sticky, and replicate it across several Terminals. I'd welcome any elegant suggestions you have for dealing with that :-)



[ Reply to This | # ]
ssh-agent
Authored by: sjk on Sep 23, '05 07:58:18PM
Not sure I completely understand your problem with ssh-agent but SSHKeychain might be a solution for it. Highly recommended, even with an intermittent memory leak on 10.3.x (haven't seen that on 10.4.x).

[ Reply to This | # ]
Color Terminal screens and ssh revisited yet again
Authored by: capacity on Nov 13, '07 02:27:52PM
This script broke under 10.5 for me because the RGB values for "special" colors have apparently changed in Terminal.app. I used colorwrap.sh getcolor to re-get my colors and set the new values in colorwrap.sh and almost everything is back to normal.

However, I cannot get window background color to go partially transparent using a negative value as the 4th color argument as described in this hint (e.g., {57212, 56949, 40762, -5000}), so whenever I ssh to a host my window goes completely opaque (but I can't say I remember if this worked under 10.4.x or not...).

[ Reply to This | # ]
Color Terminal screens and ssh revisited yet again
Authored by: capacity on Nov 13, '07 06:49:39PM
Also, I can't figure out why the echo command now (under 10.5) prints the -n -e flags when it didn't used to. I removed them, and despite their apparent necessity from checking out the man page (or at least the need for -n, not sure what the -e is for), colorwrap.sh seems to still function normally. Maybe printf should be used instead.

[ Reply to This | # ]
Color Terminal screens and ssh revisited yet again
Authored by: leono on May 14, '08 12:18:11PM

To fix the script in 10.5, replace all negative numbers in your color strings by adding 65536 to them. E.g. -1 become 65535.

Apparently the older version of Applescript in 10.4 had a bug that caused the higher numbers to be interpreted as signed integers, rolling them over into the negative numbers.



[ Reply to This | # ]
Color Terminal screens and ssh revisited yet again
Authored by: jaserh on Nov 08, '09 04:50:16AM

This no longer seems to work for me in Snow Leopard. I get errors like:

247:263: execution error: Terminal got an error: Can’t get window 1 whose name contains "ssh -C -c blowfish -g -i /Users/jason/.ssh/id_rsa jasonh@SOMESERVER.com_342". Invalid index. (-1719)

Anyone else having this problem?



[ Reply to This | # ]
Color Terminal screens and ssh revisited yet again
Authored by: mael on Feb 03, '10 03:43:37PM
Yes. Thats because AppleScript has been slightly changed in 10.6. I don't know how to fix the original script, but I'm working with an extremely simplified version since two years and it still works in 10.6 just fine. You might find it even better documented and easier to maintain. But then, maybe not... :-) Remember to customize the script where it says so.

#!/bin/sh

# -----------------------------------------------------------------------------
#  This script will change the text and background colors of a command
#  depending on the command (ssh, sudo, su, ...)
#  or its parameters (ssh someserver, sudo somecommand, ...)
#
#  To do so, it replaces the command itself with an alias, like
#    alias ssh='/usr/local/bin/colorwrap \ssh $*'
#  in .bashrc.
#
#  Colors are either named by their ANSI name (black, white)
#  or as a rgb-tupel with 3 values in hex (00FF33).
#  To get a specific color, start the DigitalColor Meter.app,
#  use "RGB as Decimal, 16-bit"
#  and klick on the desired color on the screen to find its value.
#
#  The currently used colors can be read by using the command
#    colorwrap getcolor
#  Transparencies will *not* be detected!
#
#  Since getting and setting the colors is done via AppleScript,
#  and since AppleScript relies on the current name of the window,
#  this script does sometimes have problems with Visor.
#
#  OS X Terminal from 10.5 onwards:
#  Since *setting* the transparency is not supported anymore 
#  (although it is still possible to do so in the preferences),
#  all transparencies are lost once this script is activated by any commands,
#  until the window is closed and opened again.
#
#  Idea and original Script: JD Smith and Eric Kow
#  Source: http://www.macosxhints.com/article.p0hp?story=20050920183403172
#
#  last edited: 05.06.2009
#  maelcum
# -----------------------------------------------------------------------------



# ----------------------------------------------------------------------
#  Definition of properties
# ----------------------------------------------------------------------

PEACH="{65535, 53713, 41377}"
BLUE="{32382, 40092, 54227}"
RED="{65021, 30840, 28527}"
GREEN="green"
LIGHT_GRAY="{26214, 26214, 26214}"
BLACK="black"



# ----------------------------------------------------------------------
#  Definition of functions
# ----------------------------------------------------------------------


#  function to get the currently used colors
#
fnc_get_fg_color() {
    echo `osascript -s s -e "tell application \"Terminal\" to tell front window to get its normal text color"`
}
#
fnc_get_bg_color() {
    echo `osascript -s s -e "tell application \"Terminal\" to tell front window to get its background color"`
}


#  function to assign (not set!) the new text and background color,
#  depending on the command that has been passed as a parameter
#
fnc_select_fg_color() {
    case "${1}" in
	*someurl.com)   NEW_FG_COLOR=$BLUE;;    <<<  CUSTOMIZE THIS
	*router1)       NEW_FG_COLOR=$PEACH;;   <<<  CUSTOMIZE THIS
	*someserver)	NEW_FG_COLOR=$PEACH;;   <<<  CUSTOMIZE THIS
        *192.168.0.*)   NEW_FG_COLOR=$PEACH;;   <<<  CUSTOMIZE THIS
	*sudo*)		NEW_FG_COLOR=$RED;;
	su*)	        NEW_FG_COLOR=$RED;;
	*)	        NEW_FG_COLOR=$GREEN;;
    esac
    echo ${NEW_FG_COLOR}
}
#
fnc_select_bg_color() {
    case "${1}" in
	sudo*)		NEW_BG_COLOR=$BLACK;;
	su*)	        NEW_BG_COLOR=$BLACK;;
	*)	        NEW_BG_COLOR=$BLACK;;
    esac
    echo ${NEW_BG_COLOR}
}


#  function to quote the color, if necessary
#
fnc_quote_color() {
    THE_COLOR=$*
    case ${THE_COLOR} in 
	\"*\") ;;
	'{'*'}') ;;
	*) THE_COLOR='"'${THE_COLOR}'"'
	;;
    esac
    echo ${THE_COLOR}
}


#  function to set (activate) the previously assigned colors
#
#  this function will be used to set the new colors as well as to reset the old ones back after the command finished
fnc_set_colors() {
    fg_color=`fnc_quote_color ${1}`
    bg_color=`fnc_quote_color ${2}`

    osascript -s s                                                                                      \
    -e "tell application \"Terminal\" to tell front window to set its normal text color to ${fg_color}" \
    -e "tell application \"Terminal\" to tell front window to set its background color to ${bg_color}"
}


#  function to fall back if any error happened
#
fnc_unset_colors_error() {
  # reset the color to its old value
  fnc_set_colors "${ORIGINAL_FG_COLOR}" "${ORIGINAL_BG_COLOR}"
  exit 1;
}


# -----------------------------------------------------------------------------
#  main()
# -----------------------------------------------------------------------------


#  check if any parameter has been passed, otherwise bail out
[ $# -lt 1 ] &&  { echo "usage: $0 cmd [args...]"; exit 1; }


#  save all parameters in the variable. thats the commands that should be executed once the color has been adjusted
COMMAND=$*


#  if only the current colors should be read and displayed (parameter: "getcolor")
#  then do so and exit.
if [ "${COMMAND}" == "getcolor" ]; then
    echo "Text Color is `fnc_get_fg_color`"
    echo "Background is `fnc_get_bg_color`"
    exit 0
fi


#  read and save current color settings
ORIGINAL_FG_COLOR=`fnc_get_fg_color`
ORIGINAL_BG_COLOR=`fnc_get_bg_color`


#  assign and set the new text and background color, depending on the command that has been passed as a parameter
NEW_FG_COLOR=`fnc_select_fg_color "${COMMAND}"`
NEW_BG_COLOR=`fnc_select_bg_color "${COMMAND}"`
fnc_set_colors "${NEW_FG_COLOR}" "${NEW_BG_COLOR}"


#  if an error occurs, the function to reset the colors to their original state is called.
#  the following signals will be monitored: quit, kill abort, error
trap fnc_unset_colors_error 1 2 3 6


#  execute the saved command with its parameters
#  and remember the exit status (successful or not)
${COMMAND}
EXIT_STATUS=$?


#  reset the colors to their original state
fnc_set_colors "${ORIGINAL_FG_COLOR}" "${ORIGINAL_BG_COLOR}"


# exit with the exit status of the command we ran
exit ${EXIT_STATUS}


[ Reply to This | # ]
Color Terminal screens working in 10.6
Authored by: mael on Feb 03, '10 03:46:18PM

Working version for 10.6 (and in 10.5 as well, of course):

http://www.macosxhints.com/comment.php?mode=display&format=threaded&order=ASC&pid=113493



[ Reply to This | # ]