]> www.infradead.org Git - users/dwmw2/vpnc-scripts.git/commitdiff
with iproute2, sort the routes to the VPN gateway by metric before trying to create...
authorDaniel Lenski <dlenski@gmail.com>
Wed, 30 Sep 2020 18:52:06 +0000 (11:52 -0700)
committerDaniel Lenski <dlenski@gmail.com>
Tue, 10 Nov 2020 22:57:01 +0000 (14:57 -0800)
Signed-off-by: Daniel Lenski <dlenski@gmail.com>
vpnc-script

index 067893d92ca59226f41d634a21d2679b85f55cc6..0321cf6312a30bc274859992ef8789fee68e85e9 100755 (executable)
@@ -250,7 +250,10 @@ if [ -n "$IPROUTE" ]; then
                # its address (excluding those through TUNDEV because the goal is to avoid loopback).
 
                echo "$VPNGATEWAY" | grep -q : && FAMILY=-6 ROOT=::/0 || FAMILY=-4 ROOT=0/0
-               $IPROUTE $FAMILY route show to "$VPNGATEWAY" root "$ROOT" | grep -v "dev $TUNDEV" | cut -d' ' -f2- |
+               # put metric in front, sort by metric, then chop off first two fields (metric and destination)
+               $IPROUTE $FAMILY route show to "$VPNGATEWAY" root "$ROOT" |
+                   awk '/dev '"$TUNDEV"'/ { next; } { printf "%s %s\n", (match($0, /metric ([^ ]+)/) ? substr($0, RSTART+7, RLENGTH-7) : 4294967295), $0; }' |
+                   sort -n | cut -d' ' -f3- |
                while read LINE ; do
                        # We do not want to use 'replace', since a route to the gateway that already
                        # exists is mostly likely the correct one (e.g. the case of a reconnect attempt