On Thu, Mar 23, 2006 at 03:07:08PM +0100, Jules Colding wrote:
> On Thu, 2006-03-23 at 14:36 +0100, Toby 'qubit' Cubitt wrote:
> > > > >Having an SSH session on another machine and forgetting ll about it.
> > > > >
> > > > >Please forgive my stupidity here.
> > > > >
> > > > >Sorry,
> > > > >  jules
> > > > >
> > > > I thought only I could do that.  Funny ain't it?
> > > 
> > > Not when you do it in public ;-)
> > 
> > I used to give the shell prompts different colours on different
> > machines to help avoid this. Or rather, the local one would always be
> > the same colour, but shells under ssh sessions were colour-coded by
> > machine.
> > 
> > I've lost the script I wrote for this somewhere in the mists of time
> > (if I remember right, it was copied and hacked from a bash prompt
> > example that colour-coded according to the login type: ssh, telnet,
> > local, etc.)
> > 
> > Someday I might get round to recreating it...
> 
> That would be helpful.


Here you go. It also checks if you're root. Save it as something
suitable somewhere in your $PATH, (e.g. ~/bin/bash_prompt), modify to
suit your setup, then do:

  source ~/bin/bash_prompt
  colour_code_prompt
  unset colour_code_prompt

either from the shell or in your .bashrc to load it.

Use at your own risk, since I've only just written it, and haven't
tested it very heavily! (When I've used it a bit to check it works
properly, I might document it a bit and put it on my web site.)

Toby


--------------
 bash_prompt:
--------------

#!/bin/bash

function colour_code_prompt
{
    # set up some colour escape variables
    BLUE="\[\033[1;34m\]"
    GREEN="\[\033[1;32m\]"
    CYAN="\[\033[1;36m\]"
    RED="\[\033[1;31m\]"
    MAGENTA="\[\033[1;35m\]"
    YELLOW="\[\033[1;33m\]"
    WHITE="\[\033[1;37m\]"
    GREY="\[\033[00m\]"
    
    
    # if logged in via ssh, choose colours according to host and user
    if [ -n "$SSH_CLIENT" ]; then
        if [ "$EUID" == "0" ]; then
            case "$(hostname -f)" in
                box1.some.domain)
                    COLOUR1=$RED
                    COLOUR2=$GREEN
                    ;;
                box2*)
                    COLOUR1=$RED
                    COLOUR2=$YELLOW
                    ;;
                *)
                    COLOUR1=$RED
                    COLOUR2=$MAGENTA
                    ;;
            esac
            
        else
            case "$(hostname -f)" in
                box1.some.domain)
                    COLOUR1=$GREEN
                    COLOUR2=$CYAN
                    ;;
                box2*)
                    COLOUR1=$YELLOW
                    COLOUR2=$BLUE
                    ;;
                *.some.other.domain)
                    COLOUR1=$CYAN
                    COLOUR2=$RED
                    ;;
                *)
                    COLOUR1=$MAGENTA
                    COLOUR2=$BLUE
                    ;;
            esac
        fi
        
    # if logged in locally as root, use different colours
    elif [ "$EUID" == "0" ]; then
        COLOUR1=$RED
        COLOUR2=$BLUE
        
    # otherwise, use default colours
    else
        COLOUR1=$GREEN
        COLOUR2=$BLUE
    fi

    # set the prompt
    export PS1="[EMAIL PROTECTED] $COLOUR2\w \$ $GREY"
}


-- 
PhD Student
Quantum Information Theory group
Max Planck Institute for Quantum Optics
Garching, Germany

email: [EMAIL PROTECTED]
web: www.dr-qubit.org
-- 
gentoo-user@gentoo.org mailing list

Reply via email to