How to automatically switch ssh config based on local subnet?

前端 未结 5 2007
无人及你
无人及你 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:13

    My solution to this problem is the following:

    Host myserver
        HostName [internal IP]
        ...
        
    Match Host [internal IP] !Exec "nc -w1 -q0 %h %p < /dev/null"
        ProxyCommand ssh jumphost -W %h:%p
    

    It's important to have the Host myserver lines first, so the SSH client will know the IP address.

    In the Match expression,

    • The Host option matches on that IP. (It accepts *, so you can match to /8, /16 or /24 subnets too.)
    • The Exec option executes a netcat with a 1 second timeout to test if the SSH port is open. If not, the ProxyCommand is used.

    This is the clearest way I found to actually test if you need a jumphost or not. If your network is lagging, you can set higher timeouts, of course. See man ssh_config for more details.

提交回复
热议问题