0

I need to ssh to multiple hosts having this configuration:

Protocol 2
ExitOnForwardFailure yes
ServerAliveCountMax 1
ServerAliveInterval 3
TCPKeepAlive no
ControlMaster auto
ControlPath ~/.ssh/sockets/%C
ControlPersist 10m

Host targetA
  HostName      <server-ip>
  User          user-name
  LocalForward  8888 localhost:8888
  LocalForward  3306 localhost:3306

Host targetB
  HostName      <server-ip>
  User          user-name
  LocalForward  8888 localhost:8888
  LocalForward  3306 localhost:3306

After connecting to host targetA and then trying to connect to targetB I get this error:

bind [127.0.0.1]:8888: Address already in use
channel_setup_fwd_listener_tcpip: cannot listen to port: 8888

Therefore I need to kill existing connections to be available to connect to targetB and use the same ports 8888 & 3306, something like this:

lsof -ti:8888,3306 | xargs -L1 kill -9

or

ssh -O exit targetA

This works but would like to know if there is a way to simplify this process, probably with something in ~.ssh/config, and continue to have the ControlMaster and ControlPath options besides still using same ports.

Tried already something like ProxyCommand sh -c "script.sh; ssh %h %p" without success.

Your Answer

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

Browse other questions tagged or ask your own question.