Skip to content

Instantly share code, notes, and snippets.

@locriani
Last active Mar 9, 2017
Embed
What would you like to do?
SSH Terminal Color Hack
alias ssh="~/.bash/ssh.sh"
www.example.com
list your safe servers on each newline
This file lives in your home directory (not in ~/.bash)
You need at least one "safe" server in order for the script to function properly
function onExit {
osascript ~/.bash/terminal_color.scpt "Pro"
}
trap onExit EXIT
safe_server=`echo "$@" | grep -i --max-count=1 -f ~/.safe_servers | wc -l`
if [ $safe_server -gt 0 ]; then
osascript ~/.bash/terminal_color.scpt "SafeRemote"
else
osascript ~/.bash/terminal_color.scpt "Remote"
fi
/usr/bin/ssh "$@"
on run argv
tell application "Terminal" to set current settings of selected tab of window 1 to (first settings set whose name is (item 1 of argv))
end run
-- iTerm2 User?
-- change the ssh.sh script to use colors instead of theme names and use this AppleScript instead:
--
-- on run argv
-- tell application "iTerm" to set background color of current session of current terminal to (item 1 of argv)
-- end run
@locriani
Copy link
Author

locriani commented Jul 2, 2014

SSH Terminal Color Hack

This spiffy hack allows you to turn Terminal.app a different color when you SSH into a remote server.

Example

Setup for Terminal.app users

This assumes you are using the Pro settings as your default, and will setup a Remote setting with the changes you want for remote terminals. It also assumes you're using Bash as your shell.

  1. Clone your Pro settings twice in Terminal, and name the new settings Remote and SafeRemote.
  2. Change your colors! Or whatever other settings you want to denote remote terminals. I highly recommend Comic Sans.
  3. Create the directory ~/.bash if it doesn't already exist.
  4. Create the files ssh.sh and terminal_color.scpt with the contents above in ~/.bash - make sure they are executable (chmod +x filename)
  5. Setup your .safe_servers file in your home directory. This should be a list of servers it is safe to muck around with (playroom / staging servers).
  6. Add alias ssh="~/.bash/ssh.sh" to your .bash_profile

Setup for iTerm2 users

This assumes you're using Bash as your shell. I also don't use iTerm2, so ymmv.

  1. Pick some colors. If you have difficulty with this, please consult the nearest cat.
  2. Create the directory ~/.bash if it doesn't already exist.
  3. Create the files ssh.sh and terminal_color.scpt with the contents above in ~/.bash - make sure they are executable (chmod +x filename)
  4. Use the iTerm2 variant of terminal_color.scpt, and replace SafeRemote and Remote in ssh.sh with the background colors you want.
  5. Setup your .safe_servers file in your home directory. This should be a list of servers it is safe to muck around with (playroom / staging servers).
  6. Add alias ssh="~/.bash/ssh.sh to your .bash_profile

For more information, check out the blog post at http://developer.teamsnap.com/blog/2015/01/24/terminal-color-scripting/

@Emily
Copy link

Emily commented Jul 3, 2014

Such star. Much Bright.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment