NOTE: This solution only works on a Mac! It requires AppleScript and the Terminal application.
I am constantly ssh’ing into different machines and then forgetting which window is which machine.
My solution is to use different color settings for different environments. For example, when I ssh into a production machine I want the Mac Terminal Profile to change to “Red Sands”. And, when I close ssh I want it to revert back to the Profile that was in use originally. This is not a new problem and there are 100’s of similar looking solutions out there. However all the solutions I could find suffer from the same flaw — when a ssh session running in a background Window+Tab times out or gets disconnected, that window is not ‘reset’. In most cases the currently active window is reset, which is not at all what we want.
There are 2 files. Put both in /usr/local/bin and do ‘chmod +x’ on them. The code assumes that your current ssh program is in /usr/bin/ssh. If it’s somewhere else, change the 2nd to last line in the ssh file.
In the following file, modify the case statement to include any servers you have. For each server provide a Terminal Profile Name to use.
File: /usr/local/bin/ssh
#!/bin/sh
##
## Author: Darrin Skinner -- skinner@skinner.com
##
## This changes the terminal color depending on the ssh target environment
## and it changes it back to the original color when ssh exits
##
## In the case statement below, provide a search pattern
## and a Terminal Profile Name to use for each server.
##
HOSTNAME=$@
case $HOSTNAME in
dev.*) COLOR="Homebrew" ;; ## All development servers
qa.*) COLOR="Man Page" ;; ## All QA servers
train*) COLOR="Novel" ;; ## Training server
prod.*) COLOR="Red Sands";; ## All Production servers
*) COLOR="Pro" ;; ## Everything else
esac
ORIG_COLOR=`set_term_color "$COLOR"`
/usr/bin/ssh $@
ORIG_COLOR=`set_term_color "$ORIG_COLOR"`
File: /usr/local/bin/set_term_color
#!/bin/bash
##
## Author: Darrin Skinner -- skinner@skinner.com
##
## This script will change the Mac Terminal setting (i.e. the
## Terminal colors) to the requisted Profile.
## The old/original Profile name is returned.
##
## It will search through all the Terminal windows+tabs until it
## finds the one the script is running from and execute the
## new settings in that window+tab.
##
## Usage:
## set_term_color Novel
## set_term_color "Red Sands"
##
## The argument to pass is:
## The Terminal "color theme" Profile desired
## -> Ocean, Novel, Homebrew, etc
##
mySettingName=$1
myTTY=`tty`
osascript <<EOD
set mySettingName to "$mySettingName"
set myTTY to "$myTTY"
tell application "Terminal"
set allWindows to number of windows
repeat with thisWindow from 1 to allWindows
set allTabs to number of tabs of window thisWindow
repeat with thisTab from 1 to allTabs
if tty of tab thisTab of window thisWindow is myTTY then
set thisSettings to name of current settings of tab thisTab of window thisWindow
set current settings of tab thisTab of window thisWindow to settings set mySettingName
return thisSettings
end if
end repeat
end repeat
end tell
EOD