The truth is rarely pure and never simple

SSH: Where did I come from?

When you are frequently hopping between SSH hosts in different shells, you may forget which nodes you went through to actually get to the host you are on now. With the following changes, you can extend your bash prompt to show any intermediate hosts you went trough:

For this to work, add the following code to your .bashrc file:

function myssh() {
  PHCHAIN="$HCHAIN"
  if [ -z "$HCHAIN" ]; then HCHAIN=$(hostname -s); else HCHAIN="$HCHAIN:$(hostname -s)"; fi
  /usr/bin/ssh "$@" -t "HCHAIN=$HCHAIN exec bash"
  HCHAIN="$PHCHAIN"
}
alias ssh=myssh

Then, add the variable $HCHAIN into your PS1 environment variable. The output shown above can be achieved by

PS1='\[\e[37m\]$HCHAIN \[\e[32m\]\[\e[31;1m\]\h\[\e[37m\]:\[\e[32m\]\w \$\[\e[0m\] '
PS2="\u@\h:\w> "

As long as you do this on all machines you are moving across, then arbitrary hops are recorded correctly.

Leave a comment

Your email address will not be published.