Configuration Template#
# Basic Configuration: Define alias and default authentication method
Host custom-alias
User your-username
PreferredAuthentications publickey
IdentityFile "path/to/your/private_key"
# Internal Network Routing: Check internal network reachability via ping
Match host custom-alias exec "ping -n 1 -w 100 internal.ip >nul 2>&1"
HostName internal.ip
Port internal_port
# External Network Routing: Alternative when the internal network is unreachable
Match host custom-alias
HostName external.ip
Port external_port
Usage Steps#
-
Replace Configuration Items
custom-alias
: Set a memorable aliasyour-username
: Replace with your login usernamepath/to/your/private_key
: Replace with the path to your private key fileinternal.ip
: Replace with the internal IPinternal_port
: Replace with the internal portexternal.ip
: Replace with the external IPexternal_port
: Replace with the external port
-
Connection Command
ssh custom-alias
How It Works#
- The SSH client receives a connection request for
custom-alias
- Checks Match conditions in order:
- First, execute the ping command to check internal network connectivity
- If the internal network is reachable, use the internal configuration
- If the internal network is unreachable, use the external configuration
- Connect using the corresponding configuration
Notes#
- Windows system paths use backslashes
\
- Linux/Mac system paths use forward slashes
/
- Ping command parameters:
- Windows:
ping -n 1 -w 100
- Linux/Mac:
ping -c 1 -W 1
- Windows:
- Ensure the configuration file permissions are correct (Linux/Mac recommends 600)
This document is synchronized and updated to xLog by Mix Space
The original link is https://liuyaowen.cn/posts/default/202501021