Color-code Bash Prompt for Kerberos

· 146 words · 1 minute read

Every once in a while I open a terminal or SSH into a host and attempt to perform some administrative task, only to find that I, for whatever reason, have no Kerberos ticket. Rather than run klist every time I open a new shell, I thought it more convenient to simply color-code the prompt: blue if kerberized; red if not. Easy enough (assuming you don’t forget to close your brackets). Just define a function in your .bashrc and call it when the shell starts.

function klist_ps1(){
local readonly BLUE_COLOR="\[\033[1;34m\]"
local readonly RED_COLOR="\[\033[1;31m\]"
local readonly RESET_COLOR="\[\033[0m\]"

if klist &>/dev/null; then
PS1="[${BLUE_COLOR}\u@\h${RESET_COLOR} \W]\$ "
else
PS1="[${RED_COLOR}\u@\h${RESET_COLOR} \W]\$ "
fi
}

klist_ps1

The problem with this is it only applies when the shell is first called. This is easily remedied by simply aliasing the relevant programs to call klist_ps1 as well.

alias kinit="kinit && klist_ps1"
alias kdestroy="kdestroy && klist_ps1"