how to add ip routes with host name instead of ip address?

I want to add IP routes for host that I do not have the IP address for. I basically want tell macOS to search for a host in a specific interface:

sudo route add -host <myHostname> -iface en7

Background: I have set up WiFi to be my primary network interface. But I want to be able to access host that are only available in the wired network with the interface en7. Therefore I wanted to add such a route to the system.

The command above results in the error: route: bad address: myHostname

Any ideas?


You can't do this; this is not the way IP routing works.

DNS is name resolution. It's nothing more than an address book of names pointing to some numbers (IPs)

myhost.com --------> 111.222.333.444

DNS tells you what the address is, not how to get there. IP routing is the mechanism of how you get from one point to the next. It's the same difference between "Contacts" and "Maps" in your iPhone.

I basically want tell macOS to search for a host in a specific interface:

You can't tell an interface what "hosts" (names) are there. You tell it what networks are accessible. Networks are defined by IP address(es).

For instance:

  • en0 has an IP of 192.168.1.4 and is on the network 192.168.1.0/24
  • en1 has an IP of 10.0.0.9 and is is on the network 10.0.0.0/24
  • A subnetwork, 10.0.5.0/24 is attached (via switch) on the 10.0.0.0/24 network

     en0+------------> 192.168.1.0/24
    
    
     en1+------------> 10.0.0.0/24
                                 +
                                 |
                                 +------> 10.0.5.0/24
    

Now, you add a host called foo.bar in DNS and it has an IP address of 10.0.0.12. Your system will know to go through interface en1 to get to it.

If another host, called foobar.foo had an IP address of 10.0.5.12, again, it would know to go through en1 because that route would be defined.

To accomplish what you're looking to do, your host has to be able to get name resolution across two separate domains (not interfaces). You can accomplish this two ways:

  • update your hosts file manually
  • have a multi-horizon DNS server (aware of both domains/networks)