4

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.

3
  • When you say "every new session", does that just mean interactive sessions (where you get a command line prompt on the remote server)? Or does it include non-interactive sessions? How about scp sessions? sftp sessions? Does this command produce output to the terminal?
    – Kenster
    Sep 26, 2014 at 14:00
  • @Kenster: Sorry, yes. I meant every interactive session (so normal ssh foo, and ssh foo -t <cmd>, but not scp/anything else that doesn't get a pty allocated. Edited question to clarify.
    – Shabble
    Sep 27, 2014 at 14:47
  • automatically run a command every time I successfully connect – And this is exactly what happens. man 5 ssh_config says about ControlMaster: Enables the sharing of multiple sessions over a single network connection. So there is only one connection. The master ssh process creates it (connects), any consecutive ssh uses it. I understand what you want to achieve; my point is you're using the word "connection" where "session" is what you mean. Oct 21, 2018 at 5:52

0

You must log in to answer this question.

Browse other questions tagged .