On my work laptop, if I need to SSH to a non-work machine, I need to go through a proxy server, but only when I'm on the work network (either in the office or on the VPN). I do this by setting a ProxyCommand line in my SSH config file. When I'm not in the office or on the WAN, though, I don't want to use the proxy. Is there an easy way to automate the decision to use or not use the proxy?

closed as off-topic by mdpc, yoonix, kasperd, MadHatter, Jenny D Dec 23 '17 at 13:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on Server Fault must be about managing information technology systems in a business environment. Home and end-user computing questions may be asked on Super User, and questions about development, testing and development tools may be asked on Stack Overflow." – mdpc, yoonix, MadHatter, Jenny D
If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 1 down vote accepted

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
  • Worked like a charm, thank you! – Joe Casadonte Dec 26 '17 at 19:56

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