Search code examples
gitsshopenssh

Using git with SSH and (sometimes) corkscrew


I need to use git over SSH (it's a self-imposed limitation, but one I am loathe to break). If my laptop is sitting on my home network, it all works great. When I'm at work or logged in via VPN, however, I would need to use corkscrew to access my remote repository, which I can set up without problems.

I would like to remain lazy and not specify where I am pulling from/pushing to each time, if possible, so my question is: how can I configure SSH to only use corkscrew when needed (based on, for example, current IP address)? Alternately, is there a way I can have git detect whether or not to pull from/push to a particular host based on IP address?

Thanks!


Solution

  • Looking through my old questions, I realized I resolved this years ago (via questions on ServerFault / SuperUser) and never posted an answer.

    The answer is compliments of kasperd over on ServerFault.

    In your .ssh/config use the Match keyword to create a conditional section.

    The Match keyword takes different arguments, one of them is exec which will allow you to run an external command and use the return value of that command to decide whether this section applies or not.

    You can use that to run a script that inspects whatever network settings you can use to tell the difference between the different networks. The script needs to return 0 when a ProxyCommand is needed and any non-zero value when a ProxyCommand is not needed.

    Here is an example of what the config could look like:

    Match host external-host.example.com exec /home/joe/bin/need-proxy
      ProxyCommand /home/joe/bin/proxy-script %h %p
    

    In my specific case, I just ping a machine that I know is only reachable when I'm VPN'd in, and use ping's exit code as need-proxy's exit code.