I'd like to automatically run a command every time I successfully connect to a remote host via ssh.
I can do this for the first connection to each host using the PermitLocalCommand yes and LocalCommand ~/bin/ssh-notify-me entries in my ~/.ssh/config, similar to this answer but I am also using a quite aggressive ssh multiplexing configuration (approximately the following):
Host *
PermitLocalCommand yes
ServerAliveCountMax 2
ServerAliveInterval 5
ControlPath ~/.ssh/mux/%r@%h:%p
ControlMaster auto
ControlPersist 1h
With this setup, the LocalCommand is only executed for the first connection before a control master/mux socket exists. Thereafter, I connect, but the command does not run.
Is there any way (without disabling muxing) to ensure that LocalCommand or some equivalent occurs for every new interactive session opened?
Wrapping ssh binary either in a shell function or script seems like it wouldn't work because it can't know if the connect was successful until after it's completed.