My script needs configure SNMP trap destinations for a number of hosts which exist different networks. It is therefor important that the trap destination address is of the interface that is accessible to the remote node.
I don’t really want to parse the linux kernel routing table as it’s likely to be fragile and break easily. Is there anyway to interrogate the kernel and get it to tell me which way a packet would be routed.
Ideally there would be a python solution for this. I’ve been looking at the python bindings for libdnet but that only seems to be able to return the default gateway for the address 0.0.0.0.
The best answer for this in all scenarios is given by the Linux command
ip route get $DEST. This takes into account policy routing (multiple routing tables selected by different selectors).Syntax (from
ip route help):