Ever wanted to shutdown your workstation and...

Ever wanted to shutdown your workstation and confused some random $xterm with an ssh-session and one with a local shell? Still want to use your pretty shell color scheme on all your accounts anyway? Look no further. After managing to shutdown the wrong machine once or twice a year I finally took the time to build some visual safeguards into my ZSH.

Colors, Yay

However, since both ssh sessions and X terminal emulators allocate the same kind of pseudo terminal the best thing I came up with so far was iterating over the chain of parent processes until finding init or sshd. If anyone got a cleaner solution for this problem please tell me ;-)

.zshrc

function ppid_of() {
	grep ^PPid /proc/$1/status | awk '{print $2}'
}
 
function is_ssh_login() {
	pid=$$
 
	while [ $pid != 1 ]; do
		if `grep -q sshd /proc/$pid/cmdline` ; then
			return 0
		fi
		pid=`ppid_of $pid`
	done
 
	return -1
}

.zshrc

## see console_codes(4) for number<->color relations
# red foreground
root_color=31
# green foreground
user_color=32
# blue foreground
cwd_color=34
 
 
if [ `id -u` == 0 ]; then
	hostname_color=$root_color
else
	hostname_color=$user_color
fi
 
if is_ssh_login ; then
	# background color = foreground color
	hostname_color=`expr $hostname_color + 10`
fi
 
PROMPT=`echo "%{\033[01;${hostname_color}m%}%n@%m%{\033[00m%}:%b%{\033[01;${cwd_color}m%}%~%{\033[00m%} %# "`

Discussion

Michael SchutteMichael Schutte, 2008/08/16 14:10

Have you tried checking for $SSH_CONNECTION?

demoddemod, 2008/08/16 14:58

d'oh! I'm pretty sure that I checked the environment variables before writing that function but obviously I somehow missed that – thanks for pointing this out ;)

Enter your comment
FZYIT
 
blog/2008/07/shutting_down_your_workstation.txt · Last modified: 2008/12/06 17:39 by demod