How can I trick SSH to connect using different configurations based on current location?

How can I trick ssh to connect using different configurations based on current location?

  • 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.

  • Answer:

    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 the ProxyCommand for the fourth nickname. Now ssh server1 gets proxied to ssh server1[abc] depending on context. More on ProxyCommand at http://undeadly.org/cgi?action=article&sid=20070925181947.

sorin at Server Fault Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.