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.

I want to have access to my home machine from my laptop from either home or the office. At the office, I have to use a gateway. For this, I added a proxycommand entry in my .ssh/config which work fine from the office. However, if I want to use this from home, I don't want to depend on the office gateway. Is there a way to automate this?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Yes and no.

First, the no. There's nothing you can put in your ~/.ssh/config file which will make different settings active based on external factors like what network you're on or if a file exists, etc. SSH's config file is a static file.

Now the yes. You can "hack" something. It probably won't be incredibly pretty, but it may work.

Consider how ProxyCommand works. It executes a command line, and substitutes in certain strings, printf-style. That command line is where you can put your login. For example, you might have something like this in your laptop for use at the office:

host athome
    ProxyCommand ssh -x -a -q gateway nc athome.dyndns.org 22

The command launches an ssh command which connects to gateway and on that server launches netcat to connect to your actual destination. Well, instead of running ssh, you can use a shell script. For example, with this in your .ssh/config file:

host athome
    ProxyCommand bin/customconnect athome.dyndns.org

You could create a ~/bin/customconnect script:

#!/bin/sh

if /sbin/ifconfig -a | grep -q 'inet 192\.168\.'; then
        nc $1 22
else
        ssh -x -a -q gateway nc $1 22
fi

Then, assuming your network at home starts with 192.168 and your network at the office does not, the appropriate ProxyCommand will be selected by the script.

If you want other dynamic functionality, like selecting a different hostname based on network, you can obviously add this to the script as well.

Note that this solution is not without some cost. Normally, SSH launches your ProxyCommand directly. If you use this solution, there will be an extra process in play for each connection as ssh launches a shell, which in turn launches another process.

share|improve this answer

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.