R@ndom Curve

Changing Terminal color when SSH-ing to a server
Andres C. Rodriguez 2016-01-27

Change terminal colors to visually know whether the prompt is local or remote.

Animation of remote login and logout at Terminal

First World problem

I tend to have (lots of) terminals open in my desktop, and sometimes I do not know whether the terminal is inside a remote server (meaning I have SSH-ed into), or it’s just my local machine. Furthermore, I would like to differentiate between

  1. Local machine (Mac OS X)
  2. Work servers (tend to be Linux)
  3. Personal server(s)

Consider the terminal below. How do I know whether top is listing the local processes or my remote processes? I can, of course, look at the window title which normally gives some clues about which computer I am logged into (no ubuntu in this case). But it requires a level of attention that I usually don’t have. I have deleted the wrong files, or killed the wrong process because of this.

Running top on local machine

My solution: whenever I log into a server, change the color (theme really) of the terminal. How do we do this? See below.

Changing Terminal Colors

In order to change the color of the Terminal, we can use an OS script once we have SSH-ed into the server. Or we can wrap SSH so that we change the color, login into the server, and change the color back. The following bash script does just that, using the new JavaScript for Automation introduced in OS X 10.10.

#!/bin/sh

# Color the OSX Terminal app according the hostname used in ssh
# Assumes you log in with a simple "ssh myuserid@remotehost"

# Read the following posts, which gave me the inspiration/knowledge:
# http://toolsmiths.blogspot.com/2010/04/how-to-change-your-osx-terminal-color.html
# http://www.ict4g.net/adolfo/notes/2014/07/16/change-osx-terminal-settings-from-command-line.html

# Two themes I am going to be toggling back and forth
LOCAL="Homebrew"
REMOTE="Ocean"

# Change the color of the terminal to the remote configuration
LS="Application(\"Terminal\").windows[0].currentSettings = Application(\"Terminal\").settingsSets[\"$REMOTE\"]"
osascript -l JavaScript -e "$LS" > /dev/null

# Do the normal ssh passing all original parameters to `ssh`
/usr/bin/ssh "$@"

# Change the color of the terminal back to what it was before
RS="Application(\"Terminal\").windows[0].currentSettings = Application(\"Terminal\").settingsSets[\"$LOCAL\"]"
osascript -l JavaScript -e "$RS" > /dev/null

Note that the last two lines of the script will only run once I have logged out of the remote machine. On ocassion I have had weird broken connections that break this scheme, but for the most part it works very well.

Since I do not want to learn a new command I alias the ssh command to color.sh via:

alias ssh="~/.ssh/color.sh”

Stick it into your .profile and you are set. Next time you open a terminal and ssh somewhere the terminal will turn from the default theme (“Homebrew” in my case) to the remote theme (“Ocean” in my case).

References