How to selectively route traffic for one specified IP adddress through the VPN tunnel (not all traffic)?

Solution 1:

Step 1, create two plain text files named ip-up and ip-down in /etc/ppp and make the two files executable:

$ sudo touch /etc/ppp/ip-{up,down}
$ sudo chmod +x /etc/ppp/ip-{up,down}

Step 2, modify the file ip-up, add the following:

#!/bin/sh
export PATH="/bin:/sbin:/usr/sbin:/usr/bin"

OLDGW=`netstat -nr | grep '^default' | grep -v 'ppp' | sed 's/default *\([0-9\.]*\) .*/\1/'`

if [ ! -e /tmp/pptp_oldgw ]; then
    echo "${OLDGW}" > /tmp/pptp_oldgw
fi

dscacheutil -flushcache
route add 10.4.0.0/24 "${OLDGW}"

Step 3, modify the ip-down, add the following:

#!/bin/sh
export PATH="/bin:/sbin:/usr/sbin:/usr/bin"

if [ ! -e /tmp/pptp_oldgw ]; then
        exit 0
fi

ODLGW=`cat /tmp/pptp_oldgw`

route delete 10.4.0.0/24 "${OLDGW}"
rm /tmp/pptp_oldgw