What is an easy way to have a terminal use a different color based on the ssh hostname?

Using putty in windows, you can save sessions that connect to a specific host and use a certain text color ... it was very useful for me, since I work with a bunch of remote hosts, and I wonder if (should be) a way to get the terminal (in Snow Leopard) to imitate this behavior.

I am interested in how I am 1. Save the connection (for example, username@hostname.com ) and always open this connection with a certain text color (for example, # 00ff00) 2. Ideally, some terminal window detects which host it was on, and changes its color accordingly. Therefore, if I were in my usual Terminal environment and issued a successful ssh username@hostname.com , it automatically changed the text color of this terminal window (or tab) to # 00ff00

Let me know, thanks!

+6
putty terminal ssh macos
source share
2 answers

In the terminal, you can define profiles with the background color of the background, opacity, etc. Also in profiles you can specify a start command. You can configure a different profile for each host that you use with the "ssh me @thathost" startup command, but this will only work for new windows. Profiles are easy to get through Shell โ†’ New window.

+4
source share

OK, if you insist on calling ssh from the command line, this is what the trick should do: write a shell script and save it somewhere as colorssh.sh . When it starts, it looks through its arguments for the corresponding host and sets the colors of the active terminal window accordingly. Then it calls a valid ssh passing through these arguments. When ssh returns execution in a script, it returns the colors to normal.

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

As for the script itself? Here is the code:

 #!/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) username@host.example.com ) setTerminalColors "{0,65535,65535}" "{65535,0,0}" ;; username@otherhost.example.com ) setTerminalColors "{65535,65535,0}" "{0,65535,0}" ;; esac done ssh $* # back to normal setTerminalColors "{0,0,0}" "{65535,65535,65535}" 

You will need to edit the script to add new combinations of hosts and colors.

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

Technically, part of AppleScript modifies deprecated properties. You should change the colors of the window using the "set set" property, but I suspect that this will change all the windows using this set of settings, not just the current one.

In addition, this script assumes that your โ€œnormalโ€ setting is black and white. If this is not the case, you can modify the script to save the current values โ€‹โ€‹before running or use the colors from the default settings.

+10
source share

All Articles