====== 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 [[http://zsh.sourceforge.net/|ZSH]]. {{ :blog:2008:07:zshcolors.png |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 ;-) 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 } ## 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%} %# "` {{tag>sh zsh quick_and_dirty}} ~~DISCUSSION~~