Ssh – Can an .ssh/config Host have multiple HostName entries

ssh

I've setup ssh and router port forwarding so I can ssh into a computer on my home network when I'm not at home. Currently I have two entries in my .ssh/config file one for when I'm on my home network and one for when I'm not:

Host mycomputer
  HostName 192.168.X.X

Host mycomputerathome
  HostName my.no-ip.dynamic

This works but I'm wondering if I can make things easier on myself. I was hoping there's a way to list multiple HostName entries such that if the first fails it falls back to the second:

Host mycomputer
  HostName 192.168.X.X
  HostName my.no-ip.dynamic

So that it will first try to connect to a host on my local network and if that isn't present, it'll try to connect using my no-ip dynamic host name. I have tried entering two HostNames but running ssh mycomputer just blocks doing nothing.

I've turned off password authentication in favor of keys so accidentally connecting to a computer on the local network when I'm not on my home network shouldn't risk my password going anywhere it shouldn't.

Is it possible to specify fallback HostNames to try if the first one doesn't work?

Best Answer

It's ugly, but I think you could do it using the exec criterion to Match on the exit status of a port knock e.g.

Host mycomputer
  Match exec "nc -z 192.168.1.11 %p"
    HostName 192.168.1.11
  Match !exec "nc -z 192.168.1.11 %p"
    HostName my.no-ip.dynamic

Note that this can't really tell whether you're on "your" home network - just that you're on a private LAN segment with the same address range that happens to have a service listening on the same address/port.

Related Question