0

This is a bit of a long shot, but what would be the best way of getting ssh to, if a connection fails, run a command, and then retry to connect.

To share my use case, I am ssh-ing over an ssh tunnel to access a computer behind a firewall, meaning in my ~/.ssh/config is:

Host tunnel
    HostName 192.168.1.27

Host target
    HostName localhost
    Port 2003

and in order for ssh target to work, I would need to first have ssh -N -L 2003:localhost:22 tunnel being executed in the background. I've resolved to doing this with a single command: screen -d -m ssh -N -L 2003:localhost:22 tunnel.

It's not too annoying to run that command when ssh target fails, but it would be nice if ssh could somehow automatically run it when the connection fails.

1
  • Already answered in a similar question here Jul 29, 2015 at 14:43

1 Answer 1

0

Much better solution then tunnelling is using ProxyCommand, as described here: Forward SSH traffic through a middle machine

To match your use case:

Host tunnel
  HostName 192.168.1.27
Host target
  HostName localhost
  Port 2003
  ProxyCommand ssh tunnel -W %h:%p

or

  ProxyCommand ssh tunnel nc %h %p

Then doing just ssh target does everything for you.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .