Iptables – list all route tables

iproute2iptablesrouting

I need to know how to list the IDs of all route tables. For example, I can run:

ip rule add fwmark 2 table 104
ip route add dev eth0 default via 192.168.3.7 table 104

A call to ip rule list shows:

0:  from all lookup local 
32765:  from all fwmark 0x2 lookup 104 
32766:  from all lookup main 
32767:  from all lookup default

And a call to ip route show table 104 shows:

default via 192.168.3.7 dev eth0

If I then call ip rule del table 104, a subsequent call to ip rule list shows:

0:  from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default

However, a call to ip route show table 104 still shows:

default via 192.168.3.7 dev eth0

I know that I can flush the table using ip route flush table 104. I'd like to be able to flush all tables that are not local, main, and default. Thus I want to be able to list the existing tables.

I've seen people use cat /etc/iproute2/rt_tables, but that only produces:

#
# reserved values
#
255 local
254 main
253 default
0   unspec
#
# local
#
#1  inr.ruhep

What can I do to get all the table names that currently exist? Thanks in advance!

Best Answer

There exists a way to list all routing entries of all tables. ip route show table all

Using some shell piping magic, you can extract all table names and IDs like this:

ip route show table all | grep "table" | sed 's/.*\(table.*\)/\1/g' | awk '{print $2}' | sort | uniq

or

ip route show table all | grep -Po 'table \K[^\s]+' | sort -u

If you only care about the numeric table names, add some grep filtering:

ip route show table all | grep "table" | sed 's/.*\(table.*\)/\1/g' | awk '{print $2}' | sort | uniq | grep -e "[0-9]"

or

ip route show table all | grep -Po 'table \K[^\s]+' | sort -u | grep -e "[0-9]"