How do I know which interface will be used for a specific remote address?
Say, I have two interfaces. One of them is 192.168.0.50/24, the other is 10.0.2.30/21 and all traffic is routed through it. So, for 192.168.0.100 I expect 192.168.0.50, for 10.0.3.40 or 8.8.8.8 I expect 10.0.2.30.
I'd like to ask the OS, as it somehow binds the address to a specific interface when I do socket.connect
or socket.sendto
. So how do I find out the address the OS would bind the socket on socket.connect
without doing socket.connect
?
There are two goals: to "test" the routing table and to bind socket to a specific port and the adapter that is accessible by the peer.
Unlike in Can Python select what network adapter when opening a socket?, I don't have adapters with overlapping addresses and I don't know the address of the adapter that socket would be bound to. My goal is to find this address.