Hi,

I was wondering if there was any tool like route, netstat or even arp which
tells you which specific interface a packet is using to get out of my machine to
a specific destination which lies in a different network (IPv4 address known) when
the application sends it out? (Layer 3 packet).

The best I can think of is to act as a routing table and rely on netstat -rn
to list out my subnets and their corresponding interfaces used.
I want to write a small script to figure this out. I was able to achieve this for destination interfaces that lie on the same ethernet. eg: Source: 192.168.1.xx, Destination: 192.168.1.yy by using arp.

This is what I am doing:

MY_IP=`arp $DEST_IP | tail -1 | awk -F' ' '{print $NF}'`

I get the interface used for an outgoing packet using "arp DEST_IP".

This works beautifully for two machines on the same ethernet. But if I have my destination on a different network, how do I achieve something like above?