1 |
On Thu, Mar 23, 2006 at 03:07:08PM +0100, Jules Colding wrote: |
2 |
> On Thu, 2006-03-23 at 14:36 +0100, Toby 'qubit' Cubitt wrote: |
3 |
> > > > >Having an SSH session on another machine and forgetting ll about it. |
4 |
> > > > > |
5 |
> > > > >Please forgive my stupidity here. |
6 |
> > > > > |
7 |
> > > > >Sorry, |
8 |
> > > > > jules |
9 |
> > > > > |
10 |
> > > > I thought only I could do that. Funny ain't it? |
11 |
> > > |
12 |
> > > Not when you do it in public ;-) |
13 |
> > |
14 |
> > I used to give the shell prompts different colours on different |
15 |
> > machines to help avoid this. Or rather, the local one would always be |
16 |
> > the same colour, but shells under ssh sessions were colour-coded by |
17 |
> > machine. |
18 |
> > |
19 |
> > I've lost the script I wrote for this somewhere in the mists of time |
20 |
> > (if I remember right, it was copied and hacked from a bash prompt |
21 |
> > example that colour-coded according to the login type: ssh, telnet, |
22 |
> > local, etc.) |
23 |
> > |
24 |
> > Someday I might get round to recreating it... |
25 |
> |
26 |
> That would be helpful. |
27 |
|
28 |
|
29 |
Here you go. It also checks if you're root. Save it as something |
30 |
suitable somewhere in your $PATH, (e.g. ~/bin/bash_prompt), modify to |
31 |
suit your setup, then do: |
32 |
|
33 |
source ~/bin/bash_prompt |
34 |
colour_code_prompt |
35 |
unset colour_code_prompt |
36 |
|
37 |
either from the shell or in your .bashrc to load it. |
38 |
|
39 |
Use at your own risk, since I've only just written it, and haven't |
40 |
tested it very heavily! (When I've used it a bit to check it works |
41 |
properly, I might document it a bit and put it on my web site.) |
42 |
|
43 |
Toby |
44 |
|
45 |
|
46 |
-------------- |
47 |
bash_prompt: |
48 |
-------------- |
49 |
|
50 |
#!/bin/bash |
51 |
|
52 |
function colour_code_prompt |
53 |
{ |
54 |
# set up some colour escape variables |
55 |
BLUE="\[\033[1;34m\]" |
56 |
GREEN="\[\033[1;32m\]" |
57 |
CYAN="\[\033[1;36m\]" |
58 |
RED="\[\033[1;31m\]" |
59 |
MAGENTA="\[\033[1;35m\]" |
60 |
YELLOW="\[\033[1;33m\]" |
61 |
WHITE="\[\033[1;37m\]" |
62 |
GREY="\[\033[00m\]" |
63 |
|
64 |
|
65 |
# if logged in via ssh, choose colours according to host and user |
66 |
if [ -n "$SSH_CLIENT" ]; then |
67 |
if [ "$EUID" == "0" ]; then |
68 |
case "$(hostname -f)" in |
69 |
box1.some.domain) |
70 |
COLOUR1=$RED |
71 |
COLOUR2=$GREEN |
72 |
;; |
73 |
box2*) |
74 |
COLOUR1=$RED |
75 |
COLOUR2=$YELLOW |
76 |
;; |
77 |
*) |
78 |
COLOUR1=$RED |
79 |
COLOUR2=$MAGENTA |
80 |
;; |
81 |
esac |
82 |
|
83 |
else |
84 |
case "$(hostname -f)" in |
85 |
box1.some.domain) |
86 |
COLOUR1=$GREEN |
87 |
COLOUR2=$CYAN |
88 |
;; |
89 |
box2*) |
90 |
COLOUR1=$YELLOW |
91 |
COLOUR2=$BLUE |
92 |
;; |
93 |
*.some.other.domain) |
94 |
COLOUR1=$CYAN |
95 |
COLOUR2=$RED |
96 |
;; |
97 |
*) |
98 |
COLOUR1=$MAGENTA |
99 |
COLOUR2=$BLUE |
100 |
;; |
101 |
esac |
102 |
fi |
103 |
|
104 |
# if logged in locally as root, use different colours |
105 |
elif [ "$EUID" == "0" ]; then |
106 |
COLOUR1=$RED |
107 |
COLOUR2=$BLUE |
108 |
|
109 |
# otherwise, use default colours |
110 |
else |
111 |
COLOUR1=$GREEN |
112 |
COLOUR2=$BLUE |
113 |
fi |
114 |
|
115 |
# set the prompt |
116 |
export PS1="$COLOUR1\u@\h $COLOUR2\w \$ $GREY" |
117 |
} |
118 |
|
119 |
|
120 |
-- |
121 |
PhD Student |
122 |
Quantum Information Theory group |
123 |
Max Planck Institute for Quantum Optics |
124 |
Garching, Germany |
125 |
|
126 |
email: toby@××××××××.org |
127 |
web: www.dr-qubit.org |
128 |
-- |
129 |
gentoo-user@g.o mailing list |