Skip to content

Instantly share code, notes, and snippets.

@zhiguangwang
Last active January 27, 2016 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhiguangwang/93f5f7cc7fb2ee52bf14 to your computer and use it in GitHub Desktop.
Save zhiguangwang/93f5f7cc7fb2ee52bf14 to your computer and use it in GitHub Desktop.
Shell scripts for bypassing PPTP VPN with intranet IP addresses (Mac OS X).

Add shell scripts

Add the following scripts under /etc/ppp/ (requires root privilege):

common

#!/bin/sh

# add/delete route only if device is in a specific intranet IP address block,
# change this value to YOUR network.
INTRANET_BLOCK='10.230'

# Element order in array determines interface priority
# you can add more interfaces to this list to meet your needs
NETWORK_INTERFACES=(
    'bridge0'   # Thunderbolt bridge
    'en0'       # WiFi
)

function is_intranet()
{
    local interface=$1
    local inet=$(/sbin/ifconfig "$interface" inet | /usr/bin/grep "inet $INTRANET_BLOCK")
    if [ -n "$inet" ]; then
        return 0
    else
        return 1
    fi
}

function add_route()
{
    local interface=$1
    local default_gateway=$(/usr/sbin/netstat -rn | /usr/bin/grep default | /usr/bin/grep "$interface" | /usr/bin/awk '{print $2}')
    /sbin/route -n add -net "$INTRANET_BLOCK" "$default_gateway"
}

function delete_route()
{
    /sbin/route delete -net "$INTRANET_BLOCK"
}

ip-up

#!/bin/sh

source /etc/ppp/common

for i in "${NETWORK_INTERFACES[@]}"
do
    if is_intranet $i; then
        add_route $i
        exit 0
    fi
done

ip-down

#!/bin/sh

source /etc/ppp/common

for i in "${NETWORK_INTERFACES[@]}"
do
    if is_intranet $i; then
        delete_route $i
        exit 0
    fi
done

Change file permissions

sudo chmod 755 [script]

Verify it works

netstat -nr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment