list all route tables
Solution 1:
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]"
Solution 2:
What can I do to get all the table names that currently exist?
The file /etc/iproute2/rt_tables
is the only source of table names on the system. Internally, routing tables have integer identifiers.
The rest of your question is a bit confusing. If a table is not referenced by a rule, then it is effectively "deleted" because it has no impact on the routing of the system. So you can list all the active routing tables like this:
ip rule list | awk '/lookup/ {print $NF}'
That looks for all lookup
action in the routing rules and prints the target.
It looks like routing table are identified by a 64 bit integer. If you really, really wanted to find all routing tables with rules, even the inactive ones that have no impact on your system, you could simple iterate a loop from 1 to 2^64:
seq 0 $(echo '2 64 ^ p' | dc) | xargs -iTABLE sh -c 'echo === TABLE ===; ip route show table TABLE'
...but this will take forever and ever, because 64 bits covers a lot of space.
Solution 3:
(egrep '^\s*[0-9]' /etc/iproute2/rt_tables | awk '{print $2}' | sort -u; ip rule list | awk '/lookup/ {print $NF}' | sort -u; ip route show table all | sed 's/.*\(table.*\)/\1/g' | awk '{if($0 ~ /table/) print $2; else print "main";}' | sort -u) | sort | uniq -c
Gratitude for this thread leading to this combo that shows all the routing tables that currently contain routes, that are referenced by rules, and that are named, with a 3 next to each table that has content, is referenced, and is named or a 2 or a 1 if one or two of the three conditions apply.