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:
Screen Shot 2015-12-26 at 15.46.21

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

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

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










\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.

You may also like...

Leave a Reply

Your email address will not be published.