17

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?

  • 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
34

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.

  • Oh, I do like that. +1 – roaima Mar 23 '15 at 19:58
  • 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 0: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 1:40
  • And here's the tool set it is a part of: en.wikipedia.org/wiki/Iproute2 – leeand00 Mar 24 '15 at 5:18
  • Uhm... does it work with policy based routing? Say how do I trace port specific routing with iptables, FWMARK rules? – mlt Mar 2 at 5: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.