28

Is there a tool that debugs routing tables on a Linux machine?

I mean one that I can use by inputting an ip address into it, it'll take the existing routing table into account and output the matches from the table, so I can get an idea where the packets will go?

leeand00
  • 4,443
  • 10
  • 51
  • 78
  • 1
    Of the routes and interfaces defined for the host, you just want to know which route will be taken to a given IP? Sounds like a cool utility. If none exists, it could be written in any number of languages, even bash. – Andrew Mar 23 '15 at 15:41
  • How many routers are connected to your linux machine? Did you mean `traceroute` instead? – ott-- Mar 23 '15 at 19:20

2 Answers2

63

Use ip route get. From Configuring Network Routing :

The ip route get command is a useful feature that allows you to query the route on which the system will send packets to reach a specified IP address, for example:

# ip route get 23.6.118.140
23.6.118.140 via 10.0.2.2 dev eth0 src 10.0.2.15
cache mtu 1500 advmss 1460 hoplimit 64

In this example, packets to 23.6.118.140 are sent out of the eth0 interface via the gateway 10.0.2.2.

Mark Plotnick
  • 24,913
  • 2
  • 59
  • 81
  • Hmm so on OpenWRT I just have to install iproute2 and then maybe I can do this? ifconfig is all that comes with it by default, I'm sure I can add it in with opkg...but if that were not an option roaima's answer would be the better one, if iproute2 is available then this will work great, thank you both. – leeand00 Mar 24 '15 at 00:36
  • 1
    I don't have OpenWRT, but from looking at their documentation it looks like they now call it the `ip` package. – Mark Plotnick Mar 24 '15 at 01:40
  • And here's the tool set it is a part of: http://en.wikipedia.org/wiki/Iproute2 – leeand00 Mar 24 '15 at 05:18
  • 2
    Uhm... does it work with policy based routing? Say how do I trace port specific routing with iptables, FWMARK rules? – mlt Mar 02 '19 at 05:09
2

Save the following script somewhere useful. Call it with the IP Address that you want to test and it will tell you the corresponding route.

#!/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

Example usage

./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
roaima
  • 107,089
  • 14
  • 139
  • 261
  • 2
    gave me bogus results... get shows `8.8.8.8 dev tun0 src 172.19.0.1` but the script says `Matches 192.168.0.0 with netmask 255.255.255.0 directly on wlan0` – Ray Foss Sep 07 '20 at 02:21