Here's what I did on my Mac. For every server, I add an entry for it in my ~/.ssh/config file, e.g.
Host app13
HostName server.example.com
User tom
PermitLocalCommand yes
LocalCommand osascript %d/bin/change_terminal_colours.scpt 12 35 35
This Applescript is triggered once the SSH session is established. It sets the terminal background colour to the RGB values provided (or back to default if no colour values are provided). The potentially tricky part is to intercept the end of the SSH session to set the colours back to the defaults. For that, I created the following shell script as ~/bin/ssh as to override the default ssh command. This essentially intercepts and wraps any calls to the SSH command. I tried using aliasing and functions, but this solution worked the best:
#!/bin/bash
/usr/bin/ssh $@
osascript ~/bin/change_terminal_colours.scpt
Here's the source for the change_terminal_colours.scpt script. Put this in your ~/bin directory also:
on run argv
tell application "Terminal"
# NOTE: Color values range from 0 to 65535.
if (count of argv) > 0 then
set backgroundColor to {(item 1 of argv) * 256, (item 2 of argv) * 256, (item 3 of argv) * 256}
else
set backgroundColor to background color of default settings
end if
try
set background color of (selected tab of front window) to backgroundColor
end try
end tell
end run
I wrote this solution a week ago and have been using it ever since. I hope others find it of value. I find it works better than any of the solutions I found by Googling.