31

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?

2
  • 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
    Commented Mar 23, 2015 at 15:41
  • How many routers are connected to your linux machine? Did you mean traceroute instead?
    – ott--
    Commented Mar 23, 2015 at 19:20

2 Answers 2

68

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.

4
  • 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
    Commented Mar 24, 2015 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. Commented Mar 24, 2015 at 1:40
  • And here's the tool set it is a part of: en.wikipedia.org/wiki/Iproute2
    – leeand00
    Commented Mar 24, 2015 at 5:18
  • 2
    Uhm... does it work with policy based routing? Say how do I trace port specific routing with iptables, FWMARK rules?
    – mlt
    Commented Mar 2, 2019 at 5:09
3

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
1
  • 3
    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
    Commented Sep 7, 2020 at 2:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .