[PATCH 2/4] Helper functions for uplink dev and gateway

Gernot Hillier gernot.hillier at siemens.com
Wed Oct 11 13:40:00 PDT 2017


Add helper functions to get network device and gateway of default route of our
Internet uplink (not the VPN tunnel). (As bonus, we have get_default_dev for
/sbin/route mode to get information about currently active default route just
to complement get_default_gw.)

Signed-off-by: Gernot Hillier <gernot.hillier at siemens.com>
---
 vpnc-script | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/vpnc-script b/vpnc-script
index 75e1cda..d04bba9 100755
--- a/vpnc-script
+++ b/vpnc-script
@@ -223,6 +223,15 @@ destroy_tun_device() {
 # =========== route handling ====================================
 
 if [ -n "$IPROUTE" ]; then
+	get_uplink_gw() {
+		# route to VPNGATEWAY is always our uplink, independent if VPN tunnel is active or not
+		$IPROUTE route get "$VPNGATEWAY" | sed -ne 's/^.*via \([a-z0-9\.]*\).*$/\1/p'
+	}
+
+	get_uplink_dev() {
+		$IPROUTE route get "$VPNGATEWAY" | sed -ne 's/^.*dev \([a-z0-9]*\).*$/\1/p'
+	}
+
 	fix_ip_get_output () {
 		sed -e 's/ /\n/g' | \
 		    sed -ne '1p;/via/{N;p};/dev/{N;p};/src/{N;p};/mtu/{N;p}'
@@ -312,6 +321,31 @@ else # use route command
 		netstat -r -n | awk '/:/ { next; } /^(default|0\.0\.0\.0)/ { print $2; }'
 	}
 
+	get_uplink_gw() {
+		# If we have a specific route to VPNGATEWAY, then this is our
+		# Internet uplink (VPN tunnel active). If tunnel is not active,
+		# then the default route should be our uplink.
+		uplink_route=`netstat -r -n | grep "^$VPNGATEWAY"`
+		if [ -n "$uplink_route" ]; then
+			echo "$uplink_route" | awk '{ print $2; }'
+		else
+			get_default_gw
+		fi
+	}
+
+	get_default_dev() {
+		netstat -r -n | awk '/:/ { next; } /^(default|0\.0\.0\.0)/ { print $8; }'
+	}
+
+	get_uplink_dev() {
+		uplink_route=`netstat -r -n | grep "^$VPNGATEWAY"`
+		if [ -n "$uplink_route" ]; then
+			echo "$uplink_route" | awk '{ print $8; }'
+		else
+			get_default_dev
+		fi
+	}
+
 	set_vpngateway_route() {
 		route add -host "$VPNGATEWAY" $route_syntax_gw "`get_default_gw`"
 	}
-- 
2.12.3




More information about the openconnect-devel mailing list