22

The BSD route command has a feature that will show what route will be selected for a given host. For example:

/Users/mhaase $ route get google.com
   route to: iad23s07-in-f8.1e100.net
destination: iad23s07-in-f8.1e100.net
    gateway: 10.36.13.1
  interface: en0
      flags: <UP,GATEWAY,HOST,DONE,WASCLONED,IFSCOPE,IFREF>
 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
       0         0         0         0         0         0      1500         0 

I occasionally find this useful if I am manually messing with routing tables to make sure that the routing rules are working as expected.

The GNU version of route does not have this same "get" subcommand. Is there some equivalent or alternative for GNU/Linux?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Mark E. Haase
  • 451
  • 2
  • 5
  • 12

1 Answers1

28

There is

ip route get 74.125.137.100

but it doesn't do hostname resolution (which I think is a good thing). The command is usually available from iproute or iproute2 packages.

  • 3
    `ip route get "$(dig google.com +short)"` for hostname resolution. – jordanm Oct 31 '12 at 17:28
  • 5
    @jordanm, More like: `dig google.com +short | xargs -tn1 ip route get` – Stéphane Chazelas Oct 31 '12 at 20:16
  • 1
    @StéphaneChazelas, please let me know this above linux command required an internet or it fetches the result from any routing table/cache ? – kd12 Jul 03 '17 at 06:51
  • The command 'dig' does a DNS lookup. That may require network access unless the DNS entry is in the local cache. – WillW Mar 16 '18 at 16:28
  • > but it doesn't do hostname resolution (which I think is a good thing) — care to elaborate why you think inconvenience it causes is really worth anything? – poige Apr 01 '18 at 15:56
  • @poige I was thinking about the UNIX philosophy where I expect each tool to do one thing and do it right. When I'm troubleshooting a network routing issue, the last thing I want is to involve DNS resolution. Like mentioned by other commenters, there are specialized tools that do just that, and well. –  Apr 02 '18 at 16:06
  • 1
    What a nonsense(?) — according to your view on UNIX philosophy `ping` shouldn't have ability to resolve by DNS name as well. It should be chain-nested with `xargs` just as with example above. — Pretty absurd take on UNIX philosophy… but, well, ok, thanks for explanation. – poige Apr 02 '18 at 16:21