Conditional SSH Hostname

SSH config to connect to server via Tailscale if Tailscale is running. If Tailscale is not running fall back to a different IP address

Requirements:

Replace the following values: {SERVER_NAME}: These configuration options will be used when you type ssh {SERVER_NAME} {SERVER_USERNAME}: The username you use to connect to the server {SERVER_TAILSCALE_IP}: IP Address or DNS name {SERVER_NON_TAILSCALE_IP}: I said “IP” but “hostname” will also work here {SERVER_PRIVATE_KEY_FILENAME}: Private key to log in with

# If Tailscale is running connect via this:
Match originalhost {SERVER_NAME} exec "[ $(/Applications/Tailscale.app/Contents/MacOS/Tailscale status --json | jq -r .BackendState) != Stopped ]"
    HostName {SERVER_TAILSCALE_IP}
    User {SERVER_USERNAME}
    IdentityFile ~/.ssh/{SERVER_PRIVATE_KEY_FILENAME}

# If Tailscale is not running connect via this:
Host {SERVER_NAME} 
    HostName {SERVER_NON_TAILSCALE_IP}
    User {SERVER_USERNAME}
    IdentityFile ~/.ssh/{SERVER_PRIVATE_KEY_FILENAME}