What's the best way (programatically) to determine the local IP which would be used to make a connection to a specific destination?
By way of example, the following works:
$ tracepath -n 192.168.251.132 1: 192.168.251.140 3.816ms pmtu 1500 1: ...
$ tracepath -n 10.0.0.1 1: 10.0.0.140 1.455ms pmtu 1500 1: ...
In other words the PC I ran it on has several IP addresses (and several interfaces), but from the above I can determine that for destination 192.168.251.132 by source IP will be 192.168.251.140, but for destination 10.0.0.1 it will be 10.0.0.140.
However this is quite slow and messy, and there is surely a better way!
Mark Rogers
On 16 Mar 14:44, Mark Rogers wrote:
What's the best way (programatically) to determine the local IP which would be used to make a connection to a specific destination?
By way of example, the following works:
$ tracepath -n 192.168.251.132 1: 192.168.251.140 3.816ms pmtu 1500 1: ...
$ tracepath -n 10.0.0.1 1: 10.0.0.140 1.455ms pmtu 1500 1: ...
In other words the PC I ran it on has several IP addresses (and several interfaces), but from the above I can determine that for destination 192.168.251.132 by source IP will be 192.168.251.140, but for destination 10.0.0.1 it will be 10.0.0.140.
However this is quite slow and messy, and there is surely a better way!
ip route get <destinationip> | sed -e '1 { s#.*src ##; p; }; d'
That gives:
$ ip route get 8.8.8.8 | sed -e '1 { s#.*src ##; p; }; d' 10.124.150.80 $ ip route get 192.168.1.250 | sed -e '1 { s#.*src ##; p; }; d' 192.168.1.1 $
The ip program is part of the iproute2 package, and is "really useful" (tm).
On 16/03/12 14:54, Brett Parker wrote:
ip route get<destinationip> | sed -e '1 { s#.*src ##; p; }; d'
Brilliant, thanks!
The ip program is part of the iproute2 package, and is "really useful"
So it seems!
I'd go for the "ip route" command. On my server it gives me:
default via 172.16.0.254 dev br2 metric 100 172.16.0.0/24 dev br2 proto kernel scope link src 172.16.0.1 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
Which shows me that: Anything going to 172.16.0.0/24 will use 172.16.0.1 Anything going to 192.168.122.0/24 will use 192.168.122.1 Anything else will go via 172.16.0.254, which I can then use the above two rules to calculate the interface used (172.16.0.1 in this case)
Matt
-----Original Message----- From: main-bounces@lists.alug.org.uk [mailto:main-bounces@lists.alug.org.uk] On Behalf Of Mark Rogers Sent: 16 March 2012 14:45 To: ALUG - mailing list Subject: [ALUG] Determining "my" LAN IP
What's the best way (programatically) to determine the local IP which would be used to make a connection to a specific destination?
By way of example, the following works:
$ tracepath -n 192.168.251.132 1: 192.168.251.140 3.816ms pmtu 1500 1: ...
$ tracepath -n 10.0.0.1 1: 10.0.0.140 1.455ms pmtu 1500 1: ...
In other words the PC I ran it on has several IP addresses (and several interfaces), but from the above I can determine that for destination 192.168.251.132 by source IP will be 192.168.251.140, but for destination 10.0.0.1 it will be 10.0.0.140.
However this is quite slow and messy, and there is surely a better way!
Mark Rogers
-- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
_______________________________________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!