Reading routing tables with Python and SNMP
This little Python script uses SNMP queries to read the routing table of a router. It also extracts the interface names and the interface IP addresses and subnet masks. It requires the PySNMP and PyASN1 modules to be installed.
At the moment, this only works with VyOS and Vyatta Core. It can be altered to work with Cisco and Juniper and other routers too, but I haven't done this yet. I have used this script to read my own ADSL router's routing table. Sadly, due to inconsistencies between various vendors, it can (and will) fail often, because not all vendors use the same OIDs for the same data. I might do something about this in the future, by analyzing how different vendors store the data and attempting to either auto-detect the target type, or letting the user to specify it.
I've only tested this under Python 2.7.x, but 2to3 says Python 3.x should work. YMMV, as I haven't actually tested this because PySNMP and PyASN1 don't have Python 3 versions.
- get_routing_table.py, 5.47 KiB
The script has been tested under Debian 7 and 8 and Windows 7. It should work pretty much anywhere as long as you have Python 2.7.x installed and have also installed the PySNMP and PyASN1 modules.
If you don't know how to install Python modules, the procedure is fairly simple. After you've downloaded the two tarballs (PySNMP and PyASN1) both are installed using exactly the same commands, just the filenames change:
tar xzvf <filename> cd <newly-created directory> python setup.py install
After both have been installed, you can use this script.