Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Apr 8 '12 at 14:33

This question came from our site for professional and enthusiast programmers.

Great question. Wrong forum. – pilcrow Apr 7 '12 at 23:16
I suppose this cannot be done without some coding, am I wrong? Because you can configure a custom command in .ssh/config, means that you can code the logic in an outside script. – sorin Apr 8 '12 at 10:54
@sorin You'll need to use ProxyCommand in your ssh config, yes. But this counts as sysadmining rather than software development. – dave4420 Apr 8 '12 at 13:56

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 Stupid SSH Tricks.

share|improve this answer
Thanks, I'm already working on a python script which will do all the tricks, a smart-ssh, or maybe I should call it 3sh. I'll be back when I have the first working version. – sorin Apr 11 '12 at 8:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.