1

In my job, I have to ssh to loads of different machines. To make my life easier, I have been trying to set the colorscheme of each machine automatically so that I can tell at a glance which one I happen to be working on.

It is almost working.

~/.ssh/config:

Host some-remote-server
    Hostname some-remote-server.ac.uk
    User myusername
    IdentityFile ~/.ssh/id_rsa
    ForwardAgent yes
    ForwardX11 yes

Host some-other-remote-server
    Hostname some-other-remote-server.ac.uk
    User myusername
    IdentityFile ~/.ssh/id_rsa
    ForwardAgent yes
    ForwardX11 yes
    ProxyCommand ssh -Y some-remote-server -W %h:%p
    ControlMaster auto
    ControlPath /tmp/ssh-socket-%r@%h-%p
    ControlPersist 600

Let's pretend I want both "some-remote-server" and "some-other-remote-server" to be blue and I want "my-local-server" to be red.

Logging in is no problem; there is some code in "~/.profile" which sets the colorscheme appropriately. However, logging out is problematic.

~/.bash_logout:

if [[ $- == *i* ]]; then
  ORIGIN_HOST=`who am i | awk -F"[()]" '{print $2}'`
  $HOME/bin/set_the_colorscheme $ORIGIN_HOST
fi

When I exit an ssh session from my-local-server to some-other-remote-server, the above script identifies $ORIGIN_HOST as "some-remote-server" (b/c of the ControlMaster setting in my SSH config file) and so the colors stay blue.

Is there anyway to detect that I am using ControlMaster and find the host that issued the original ssh command?

Thanks.

  • Do you have a different home directory and/or profile file on each host or is your home directory mounted from a shared drive? – igal Oct 25 '17 at 14:00
  • @igal - I have a different home directory and profile on each host. – trubliphone Oct 25 '17 at 14:17
  • I don't think I understand what the problem is or why you need a logout script. – igal Oct 25 '17 at 14:27
  • @igal - When I ssh into a host the login script sets the color to the new host's colorscheme. When I exit from that session the logout script resets the color to the old host's colorscheme. – trubliphone Oct 25 '17 at 14:59
  • 1
    These days most people just stay logged in until the remote reboots! I use a separate terminal (xterm) per remote host, each with its own colour calculated from a hash of the remote hostname. When I logout the terminal is closed, so there is no colour to restore. For you, why not restore the colour on every single (local) command prompt by setting your shell's PS1 to include an appropriate escape sequence (assuming your terminal can understand these). – meuh Oct 25 '17 at 15:46
0

It sounds like your real goal is to restore the terminal settings after terminating an SSH session. Here is a solution that follows a slightly different path than the one you were on. This is based on solutions from other posts:

You can define a wrapper/shim function to execute some code after the SSH session terminates:

function ssh () { command ssh "$@"; echo "SSH session terminated!"; }

Using this wrapper you can reset your terminal environment based on the current host. In particular, you could do something like the following:

$ mkdir -p ~/.ssh/exit_scripts

$ cat <<HEREDOC > "${HOME}/.ssh/exit_scripts/${HOSTNAME}"
# Host-specific script goes here
echo "I'm on $(hostname)"
HEREDOC

$ function ssh () { command ssh "$@"; source "${HOME}/.ssh/exit_scripts/${HOSTNAME}"; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.