#!/bin/sh vpn="PublicVPN" # Tunnelblick VPN configuration name dev="tap" # "tun" or "tap" as defined in VPN config tries="30" # wait up to $tries * 5 seconds for gateway host IP route_add="" # additional hosts or subnets to route through the VPN route_del="" # hosts or subnets to remove from VPN routing table echo "Starting VPN $vpn..." osascript -e " tell application \"Tunnelblick\" connect \"$vpn\" end tell" try=1 while [ -z "$gw_host" -a $try -le $tries ] do sleep 5 echo "Waiting for gateway host ($try/$tries)..." gw_host="`netstat -f inet -n -r | grep -v 'ff:ff:ff:ff:ff:ff' | \ sed -n \"s/^\([0-9][0-9\.]*\)[[:space:]].*[[:space:]]UH.*$dev[0-9][[:space:]0-9]*\$/\1/p\"`" (( try++ )) done [ -z "$gw_host" ] && exit 1 echo "Found gateway host $gw_host" echo "Adding custom routes..." for net in $route_add; do sudo route add $net $gw_host; done for net in $route_del; do sudo route delete $net $gw_host; done echo "Updating dynamic DNS entry..." nsupdate-ddns.sh