Tools for Debugging Routing Tables on a Linux Machine?

32,919

Solution 1

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.

Solution 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
Share:
32,919

Related videos on Youtube

leeand00
Author by

leeand00

Projects jobdb - Creator of Open Source Job Search Document Creator/Tracker http://i9.photobucket.com/albums/a58/Maskkkk/c64nMe.jpg Received my first computer (see above) at the age of 3, wrote my first program at the age of 7. Been hooked on programming ever since.

Updated on September 18, 2022

Comments

  • leeand00
    leeand00 over 1 year

    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?

    • ott--
      ott-- about 9 years
      How many routers are connected to your linux machine? Did you mean traceroute instead?
  • roaima
    roaima about 9 years
    Oh, I do like that. +1
  • leeand00
    leeand00 about 9 years
    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.
  • Mark Plotnick
    Mark Plotnick about 9 years
    I don't have OpenWRT, but from looking at their documentation it looks like they now call it the ip package.
  • leeand00
    leeand00 about 9 years
    And here's the tool set it is a part of: en.wikipedia.org/wiki/Iproute2
  • Franz-Georg Neurieser
    Franz-Georg Neurieser about 5 years
    Uhm... does it work with policy based routing? Say how do I trace port specific routing with iptables, FWMARK rules?
  • user3821306
    user3821306 over 3 years
    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