Here is the following use case: wanting to ssh server1
in 3 scenarios:
- server1 is on current network, go for it
- server1 is not on current network and you need to use a gateway (gw1) to access it, not a big problem as you can use
~/.ssh/config
to define tunnelling for this - you are on a network that does not allow you to access the first gw1, so you'll use gw2 for that.
Desired outcome: be able to use the same command and be able to connect to the server.
You've sort of hinted at the solution already, and with dave4420's additional hint I sort of feel like I'm cheating by posting an answer.
Essentially, the
ssh-config
ProxyCommand
directive solves the problem of "how do I connect" for the two non-local cases. The remaining question boils down to "how do I use one command and ignore the problem of figuring out which case to use."Define a host nickname in your .ssh/config file for each of the three scenarios: Host server1a, Host server1b, Host server1c.
Define a fourth nickname which will be your command for connecting to this server, always: Host server1.
Write a script which implements the logic you've defined in your question. I can't pseudo-code this here because I'm not sure what tests you'd use to determine which of the three cases you're in. In all three cases, you have an ssh command to use, e.g.
ssh server1a
. Use this script as theProxyCommand
for the fourth nickname.Now
ssh server1
gets proxied tossh server1[abc]
depending on context.More on
ProxyCommand
at Stupid SSH Tricks.