How to automatically switch ssh config based on local subnet?

前端 未结 5 1997
无人及你
无人及你 2021-02-14 15:36

When I\'m on a certain network (subnet is 10.10.11.x) I need to jump through an intermediate host to reach my destination because of destination port I can\'t change and limited

5条回答
  •  感动是毒
    2021-02-14 16:07

    Instead of checking the subnet CIDR, you can check what domain suffix DHCP has given you.

    • If you're trying to use a jump box when outside the intranet, this approach is more robust than checking for IP ranges in the reserved private allocation (e.g. your home network or remote-work location uses the same block as example.com's intranet).

    • This approach is not useful if your intranet uses the same DNS suffix everywhere and you're trying to traverse subnets within the intranet. If that's your situation, use Jakuje's solution.

    Match Host web Exec "hostname -d | ! grep -q -E '^example\.com'"
        ForwardAgent yes
        ProxyCommand ssh -p 110 -q relay.example.com nc %h %p
    Host web
        HostName web.example.com
        Port 1111
    

    If you don't have a recent version of hostname that has the -d option (e.g. you're on MacOS), you can just query resolve.conf directly:

    Match Host web Exec "! grep -q -E '^\s*search[ \t]+example\.com' /etc/resolv.conf"
        ...
        ...
    

提交回复
热议问题