Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Looking for a way automatically change terminal colors when ssh-ing into remote machine and revert back to the default colors, when exiting the ssh session. Simply, want have another colors when i'm working on the remote machine.

Any idea?

ps: don't want use iTerm2, but standard Terminal.app

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

I'm using the following:

have and shell command, called myssh, with content:

PROFILE="$1";shift;
DEF="Basic" #the default profile name
echo "tell app \"Terminal\" to set current settings of first window to settings set \"${PROFILE}\""|osascript
ssh $@
echo "tell app \"Terminal\" to set current settings of first window to settings set \"${DEF}\""|osascript

and using bash aliases for the connect, like:

alias ssweb='myssh Homebrew user@web.example.com'
alias ssmail='myssh Ocean me@mail.example.com'

so, when want ssh into web server, i'm simply write into terminal ssweb, and got the "Homebrew" profile. When the ssh session ends, the aplescript set back to the "Basic".

The aliases should go into your $HOME/.profile or similiar bash-startup file.

The "Homebrew", "Ocean" etc. are the setting profiles from the Terminal.app preferences. (you can set up different fonts, encondings etc) too.

Of course, you can use it directly (without aliases) like:

myssh Pro user@another.machine.example.com
       ^        ^
       |        ssh command arguments
       terminal profile name
share|improve this answer
    
how would you do this for iterm2 ? thanks! –  Cosu Jul 11 '11 at 12:33
    
I stopped using iTerm when in "vim" after selecting some text it is cut out the selection instead of copy.. ;( So, Unfortunately, I haven't any idea about iTerm2 solution. –  jm666 Jul 11 '11 at 19:00
    
Thank you! I've been looking at various kludgy ways to do this for a while, but your method is simple and flexible. Bonus points for developing a tiny framework that can take profiles and commands from different aliases, rather than strewing around a bunch of individual scripts with hard-coded preference sets. –  octern Mar 26 '12 at 4:48
add comment

I know there's already a couple of answers, but here's an alternative:

There's no general way to change the background color, but we can use a standard way to change the window title. If you're logging in to a lot of non-"Unix server" devices (such as network devices; routers, switches, firewalls etc) then you have to use their shells and cannot install your own shell variables. So, here's another way:

function ssh() {
  H=$(echo $@ | sed -e 's/.*@//g;s/ .*//g')
  echo $@ | grep '@' && U=$(echo $@ | sed -e 's/@.*//g;s/.* //g')
  echo -ne "\033]0;${U:=$(whoami)}@${H%%.*}\007"
  unset H
  unset U
  /usr/bin/ssh $@
}

Add this to your .bashrc.

Basically, as functions and aliases are preferred to looking in the path, this takes the ssh command and uses the text before and after the "@" to set HOST and USER variables which it then uses to echo an ASCII code which is recognised by the terminal and used to set the title.

You must use "ssh user@host.blah" for this and not "ssh -l user host.blah"

If you don't provide a username it will set it as your current username (obviously if you're using a custom .ssh/config (which I use a lot) then this won't be accurate, but I think it's good enough to be useful.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.