With the following ssh_config:

Match all
  IdentityFile /opt/ssh/id_rsa
  StrictHostKeyChecking no
  ServerAliveInterval 30
  ServerAliveCountMax 3
  ControlMaster auto
  ControlPersist 300
  StreamLocalBindMask 0111
  StreamLocalBindUnlink yes
  ExitOnForwardFailure yes
  Compression yes
  BatchMode yes

Host example
  LocalForward /opt/ssh/soc/%n_mysql.socket
  ControlPath /opt/ssh/soc/%n.socket
  HostName example.com
  User user
  Port 22

Two unix-socket files are created:

# ls -1 /opt/ssh/soc

Obviously LocalForward directive doesn't like tokens...

I wonder why and is there an alternative way to achieve this?

I see that LocalCommand accepts the following tokens: %%, %C, %d, %h, %i, %l, %n, %p, %r, %T, and %u. Could it be somehow used to achieve what I want?

I tried the following config, but it only creates the ctrl socket, not the mysql one.

Host example
  PermitLocalCommand yes
  LocalCommand ssh -F /opt/ssh/config -S /opt/ssh/sockets/%n.socket -TNL /opt/ssh/sockets/%n_mysql.socket: -p %p -l %u %h
  ControlPath /opt/ssh/soc/%n.socket
  HostName example.com
  User user
  Port 22

Based on my experiments it seems that openssh people have decided to make the matters rather complicated. The following wrapper script may provide some inspiration:

#! /bin/sh --

HNAME=${1:?## Err: Please specify the host name}


ssh -fTNF "${CONFD}/config" "${HNAME}" && {
  ssh -O forward -S "${SOCKETS}/${HNAME}.socket" -TNL "${SOCKETS}/${HNAME}_mysql.socket:" "${HNAME}" &&\
  ssh -O check -S "${SOCKETS}/${HNAME}.socket" "${HNAME}"
} || exit 1

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.