Find interface for route to specific host

Solution 1:

Use ip route for this. For instance:

ip route show to match

Solution 2:

Shorter option:

ip route get

Solution 3:

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