views:

1264

answers:

4

When I ssh into a remote production server I would like the colour scheme of my terminal window to change to something brigh and scary, preferably red, to warn me that I am touching a live scary server.

How can I make it automatically detect that I have ssh'ed somewhere, and if that somewhere is on a specific list, change the colour scheme?

I want to update the Scheme of Terminal.app, not know how I would do this in a pure linux/unix env

A: 

Why not just changing the shell prompt whenever you are logged in via SSH? There are usually specific shell variables: SSH_CLIENT, SSH_CONNECTION, SSH_TTY

unexist
+2  A: 

You can set the $PS1 variable in your .bashrc.

red='\e[0;31m'
PS1="$\[${red}\]"

EDIT: To do this open the Terminal. Then say

#touch .bashrc

You can then open .bashrc in textEdit or in TextWrangler and add the previous commands.

Milhous
How exactly do I do this, and how does apple's terminal know to change its colour sceme too
Laurie Young
create a file ~/.bashrc on the server you want to change to red. (not on your mac)
davr
A: 

Xterm-compatible Unix terminals have standard escape sequences for setting the background and foreground colors. I'm not sure if Terminal.app shares them; it should.

case $HOSTNAME in
    live1|live2|live3) echo -e '\e]11;1\a' ;;
    testing1|testing2) echo -e '\e]11;2\a' ;;
esac

The second number specifies the desired color. 0=default, 1=red, 2=green, etc. So this snippet, when put in a shared .bashrc, will give you a red background on live servers and a green background on testing ones. You should also add something like this to reset the background when you log out.

on_exit () {
    echo -e '\e]11;0\a'
}
trap on_exit EXIT


EDIT: Google turned up a way to set the background color using AppleScript. Obviously, this only works when run on the same machine as Terminal.app. You can work around that with a couple wrapper functions:

set_bg_color () {
    # color values are in '{R, G, B, A}' format, all 16-bit unsigned integers (0-65535)
    osascript -e "tell application \"Terminal\" to set background color of window 1 to $1"
}

sshl () {
    set_bg_color "{45000, 0, 0, 50000}"
    ssh "$@"
    set_bg_color "{0, 0, 0, 50000}"
}

You'd need to remember to run sshl instead of ssh when connecting to a live server. Another option is to write a wrapper function for ssh that scans its arguments for known live hostnames and sets the background accordingly.

skymt
Nice idea, but it does not work in Terminal.app
Laurie Young
+5  A: 

Put following script in ~/bin/ssh (ensure ~/bin/ looked before /usr/bin/ in your PATH):

#!/bin/sh

HOSTNAME=`echo $@ | sed s/.*@//`

set_bg () {
  osascript -e "tell application \"Terminal\" to set background color of window 1 to $1"
}

on_exit () {
  set_bg "{0, 0, 0, 50000}"
}
trap on_exit EXIT

case $HOSTNAME in
  production1|production2|production3) set_bg "{45000, 0, 0, 50000}" ;;
  *) set_bg "{0, 45000, 0, 50000}" ;;
esac

/usr/bin/ssh "$@"

The script above extracts host name from line "username@host" (it assumes you login to remote hosts with "ssh user@host").

Then depending on host name it either sets red background (for production servers) or green background (for all other). As a result all your ssh windows will be with colored background.

I assume here your default background is black, so script reverts the background color back to black when you logout from remote server (see "trap on_exit").

Please, note however this script does not track chain of ssh logins from one host to another. As a result the background will be green in case you login to testing server first, then login to production from it.

Yurii Soldak