Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 stackoverflow.com Apr 8 '12 at 14:33

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

2  
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

1 Answer 1

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

 
discard

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.