Python will find the first network hop

I need to find the first network hop in a python program on Linux. I can’t just depend on the fact that the default gateway is the first move, because some network programs on the box can (or cannot) insert all the routes:

0.0.0.0/1
128.0.0.0/1

I was thinking about popping up tracepath -m 1 <some ip>and analyzing the output, but I don't like it depending on the output format of another program. Reading the routing table from / proc and applying the routing logic to it, I think this is slightly different from my scripting abilities, although I will probably try it at some point. Is there an easier way (someone else has already invented - is this a wheel in a module that I don't know about)?

+1
source share
1 answer

" " ? , . - :

$ ip route get <ip> | head -1 | awk '{ print $3 }'

off-link:

$ ip route get 8.8.8.8 | head -1 | awk '{ print $3 }'
192.168.0.1

... on-link:

$ ip route get 192.168.0.2 | head -1 | awk '{ print $3 }'
eth0

, , , , . =) , ip .

, tracepath traceroute 1- , ICMP TTL expired ( ). , .

python , libdnet.

+1

Source: https://habr.com/ru/post/1791156/


All Articles