Rob*_*b H 20 networking linux routing
在多宿主 Linux 机器上,如何找出将使用哪个网络接口将数据包发送到特定主机?我需要以编程方式执行此操作,而我不想自己解析和解释路由表。
Mic*_*ton 37
ip route为此使用。例如:
ip route show to match 198.252.206.16
Run Code Online (Sandbox Code Playgroud)
是的,正如迈克尔汉普顿所建议的那样,使用ip route. 如果你只想要界面,使用这个
ip -o route get $ip | perl -nle 'if ( /dev\s+(\S+)/ ) {print $1}'
Run Code Online (Sandbox Code Playgroud)
例如:
# ip=8.8.8.8
# iface=$( ip -o route get $ip | perl -nle 'if ( /dev\s+(\S+)/ ) {print $1}' )
# echo $iface
eth1
Run Code Online (Sandbox Code Playgroud)