在一个多宿主的Linux机器上,我怎样才能找出哪个networking接口将被用来发送数据包到特定的主机? 我需要以编程方式执行此操作,我宁愿不自己分析和解释路由表。
使用ip route
为此。 例如:
ip route show to match 198.252.206.16
是的,正如迈克尔·汉普顿build议的那样,使用ip route
如果你只想要接口,使用这个
ip -o route get $ip | perl -nle 'if ( /dev\s+(\S+)/ ) {print $1}'
例如:
# ip=8.8.8.8 # iface=$( ip -o route get $ip | perl -nle 'if ( /dev\s+(\S+)/ ) {print $1}' ) # echo $iface eth1
短:
ip route获得172.29.42.94