Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to "automatically" configure ssh to use a gateway to connect to a remote machine based on the client's IP address or something in the likes?

Suppose I'm at my laptop, which I can plug (via a wired connection) into the university network and have direct access to the cluster.uni.edu, but if I'm at home, I must hop through the gateway.uni.edu. So, to connect to the cluster from home, I can have my ~/.ssh/config like this:

Host cluster
    Host cluster.uni.edu
    ProxyCommand ssh -W %h:%p gateway.uni.edu

So I can just ssh cluster and access it transparently hopping though the gateway. The downside is that if I'm plugged to the university network, I don't need to go through the gateway, so to avoid it I would have to do something like ssh cluster.uni.edu.

Is it possible to set up this so ssh doesn't hop though the gateway it it's not needed? The criterion would be something like: If eth0 is connected and the IP is xxx.yyy.zzz.www, connect directly, otherwise use the gateway.

I think this might be a lot to ask from the configuration file and standard ssh options alone, but I would really like to avoid wrapper scripts. (Side note: I used LocalFowrard to do this kind of thing before I learned about ssh -W because I didn't like the idea of using netcat as ProxyCommand)

Thanks!

share|improve this question
    
I don't suppose just having a second Host cluster1 entry without the proxy would be enough? So ssh cluster when external, and ssh cluster1 when internal? –  Paul Feb 5 '13 at 1:35
    
you can use your own script for proxycommand to decide whether to connect via a proxy or shortcut the connection directly to the final target. –  sparkie Feb 5 '13 at 4:17
    
@Paul: yes, I could do that, but I was curious about the possibilities of ssh_config –  Elton Carvalho Feb 5 '13 at 4:29
    
@sparkie: Another possibility that occurred to me, that's why I mentioned I'm trying to avoid wrapper scripts. –  Elton Carvalho Feb 5 '13 at 4:30
1  
Ah right, no I don't think you can get this done with ssh alone, it would need some scripting. Though you could probably put the test into the ProxyCommand command line, to avoid an external script. –  Paul Feb 5 '13 at 5:21

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.