In OpenFabrics, the infiniband-diags package comes with a tool called "ibroute" that should do exactly what you're asking for. There is a script called "dump_lfts.sh" in infiniband-diags that wraps around ibroute and will output the routing tables for all the switches in your fabric.
There are other scripts out there to wrap around these for further debugging/routing analysis, but I'll leave that for another answer.
A colleague reminded me there is a mechanism in OpenSM to do this too. I haven't personally used it, but for completeness, here's the relevant portion from the OpenSM manpage:
The ability to dump switch lid matrices (aka min hops tables) to file
and later to load these is also supported.
The usage is similar to unicast forwarding tables loading from a
lfts file (introduced by 'file' routing engine), but new lid matrix
file name should be specified by -M or --lid_matrix_file option. For
opensm -R file -M ./opensm-lid-matrix.dump
The dump file is named opensm-lid-matrix.dump and will be generated in
standard opensm dump directory (/var/log by default) when
OSM_LOG_ROUTING logging flag is set.
When routing engine 'file' is activated, but the lfts file is not
specified or not cannot be open default lid matrix algorithm will be
There is also a switch forwarding tables dumper which generates a file
compatible with dump_lfts.sh output. This file can be used as
input for forwarding tables loading by 'file' routing engine. Both or
one of options -U and -M can be specified together with -R file.