Share this question

Welcome to Teachnovice Q&A, where you can ask questions and receive answers from other members of the community.

This is a collaboratively edited question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Find interface for route to specific host?

0 like 0 dislike
On a multihomed Linux machine, how can I find out what network interface will be used to send a packet to a specific host? I need to do this programmatically and I'd rather not parse and interpret the routing table myself.
asked Feb 5, 2015 by Rob H  

2 Answers

0 like 0 dislike
Best answer

Use ip route for this. For instance:

ip route show to match
answered Feb 5, 2015 by Michael Hampton♦  
Perfect! Thank you.
0 like 0 dislike

Yes, as Michael Hampton suggests, use ip route. If you only want the interface, use this

ip -o route get $ip | perl -nle 'if ( /dev\s+(\S+)/ ) {print $1}'

For example:

# ip=
# iface=$( ip -o route get $ip | perl -nle 'if ( /dev\s+(\S+)/ ) {print $1}' )
# echo $iface
answered Feb 5, 2015 by mivk