Find interface for route to specific host
Solution 1:
Use ip route
for this. For instance:
ip route show to match 198.252.206.16
Solution 2:
Shorter option:
ip route get 172.29.42.94
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=8.8.8.8
# iface=$( ip -o route get $ip | perl -nle 'if ( /dev\s+(\S+)/ ) {print $1}' )
# echo $iface
eth1