Find ping response interface via java or linux
-
22-12-2019 - |
Question
In a Multi homed machine ( multiple network interfaces and hence may have multiple IP addresses ), Using Java / Linux base ping finding which interface is actually returning the ping response as the ping is sent to all available interfaces.
One way i figured of is to use the -I and set the source interface to force ping via one interface but in case of 3 or more hybrid NIC cases we may have to deal with many pings to zero in on the correct interface.
Any direct / easier suggestions?
Solution
If all you're trying to do is determine which interface is used to get to a given destination (from https://serverfault.com/questions/188597/how-to-see-on-linux-what-network-interface-and-source-ip-address-is-used-for-a-r), parse the output of:
ip route get <destination_ip>
The interface printed there will be the same one that receives a ping response when you ping <destination_ip>
.
This only works on Linux. There may be an equivalent Windows command. I don't know.
OTHER TIPS
For script on Linux you can apply an iptables rule to log if a ping request is received on a certain interface. Afterwards or while logging you can process the log file to get out the NIC information.