Lưu tập lệnh sau đây ở đâu đó hữu ích. Gọi nó với Địa chỉ IP mà bạn muốn kiểm tra và nó sẽ cho bạn biết tuyến đường tương ứng.
#!/bin/bash
#
# Find the appropriate routing entry for a given IP address
########################################################################
########################################################################
# Calculate the base network address for a given addres and netmask
#
baseNet() {
local ADDRESS="$1" NETMASK="$2"
ipcalc -nb "$ADDRESS" "$NETMASK" | awk '$1=="Network:"{print $2}'
}
########################################################################
# Go
#
for IPADDRESS in "$@"
do
netstat -rn |
tac |
while read DESTINATION GATEWAY GENMASK FLAGS MSS WINDOW IRTT IFACE
do
NSBASENET=$(baseNet "$DESTINATION" "$GENMASK")
IPBASENET=$(baseNet "$IPADDRESS" "$GENMASK")
if test "X$NSBASENET" = "X$IPBASENET"
then
if test '0.0.0.0' = "$GATEWAY"
then
echo "Matches $DESTINATION with netmask $GENMASK directly on $IFACE"
else
echo "Matches $DESTINATION with netmask $GENMASK via $GATEWAY on $IFACE"
fi
break
fi
done
done
# All done
#
exit 0
Ví dụ sử dụng
./what-route.sh 10.0.5.6
Matches 0.0.0.0 with netmask 0.0.0.0 via 10.0.2.2 on eth0
./what-route.sh 10.0.2.6
Matches 10.0.2.0 with netmask 255.255.255.0 directly on eth0