How can I find which NIC is used to talk to a given host (except traceroute)

Hi all,

I'm having some trouble identifying what route is being used to talk to a target host. I can figure it out by looking at the routing tables but I want to automate this and don't much feel like scripting the network mask logic when I'd think there'd be a way to have the OS do it for me.

I tried using traceroute and looking for the "Multiple interfaces found, using..." message, but that fails for some zones (no raw sockets).

Does anyone know of another way to display the first hop in the route (ie the NIC name or the NIC IP)?

Thanks in advance,

Single bump, still can't figure a solution to this myself :frowning:
Any ideas out there?

I'm not a Solaris networking expert by any means, but I believe that the route command gives
you the interface that should be used:

route get <destination>

I don't know if there are exceptions or corner cases.

This output is from a virtual machine on my PC:

bash-3.2$ /sbin/ifconfig -a
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000
e1000g0: flags=1004843<UP,BROADCAST,RUNNING,MULTICAST,DHCP,IPv4> mtu 1500 index 2
        inet 10.0.2.15 netmask ffffff00 broadcast 10.0.2.255
e1000g1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
        inet 169.254.1.6 netmask ffff0000 broadcast 169.254.255.255
bash-3.2$ /sbin/route get 169.254.1.4
   route to: 169.254.1.4
destination: 169.254.0.0
       mask: 255.255.0.0
  interface: e1000g1
      flags: <UP,DONE>
 recvpipe  sendpipe  ssthresh    rtt,ms rttvar,ms  hopcount      mtu     expire
       0         0         0         0         0         0      1500         0
bash-3.2$ /sbin/route get 10.0.2.16
   route to: 10.0.2.16
destination: 10.0.2.0
       mask: 255.255.255.0
  interface: e1000g0
      flags: <UP,DONE>
 recvpipe  sendpipe  ssthresh    rtt,ms rttvar,ms  hopcount      mtu     expire
       0         0         0         0         0         0      1500         0
1 Like

It seems to list the resultant route and interface nicely when I try it on Solaris

Nice one, thanks!

Very nice, is there any equivalent to this for other systems?

On Linux using iproute2: ip route get <dest>

1 Like