Skip to content

Instantly share code, notes, and snippets.

@tianying484
Last active September 17, 2015 03:48
Show Gist options
  • Save tianying484/30e991b680fcd85bdaf9 to your computer and use it in GitHub Desktop.
Save tianying484/30e991b680fcd85bdaf9 to your computer and use it in GitHub Desktop.
vpnc-script for OS X
#!/bin/sh
#
# Originally part of vpnc source code:
# © 2005-2012 Maurice Massar, Jörg Mayer, Antonio Borneo et al.
# © 2009-2012 David Woodhouse <dwmw2@infradead.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
################
#
# List of parameters passed through environment
#* reason -- why this script was called, one of: pre-init connect disconnect reconnect
#* VPNGATEWAY -- vpn gateway address (always present)
#* TUNDEV -- tunnel device (always present)
#* INTERNAL_IP4_ADDRESS -- address (always present)
#* INTERNAL_IP4_MTU -- mtu (often unset)
#* INTERNAL_IP4_NETMASK -- netmask (often unset)
#* INTERNAL_IP4_NETMASKLEN -- netmask length (often unset)
#* INTERNAL_IP4_NETADDR -- address of network (only present if netmask is set)
#* INTERNAL_IP4_DNS -- list of dns servers
#* INTERNAL_IP4_NBNS -- list of wins servers
#* INTERNAL_IP6_ADDRESS -- IPv6 address
#* INTERNAL_IP6_NETMASK -- IPv6 netmask
#* INTERNAL_IP6_DNS -- IPv6 list of dns servers
#* CISCO_DEF_DOMAIN -- default domain name
#* CISCO_BANNER -- banner from server
#* CISCO_SPLIT_INC -- number of networks in split-network-list
#* CISCO_SPLIT_INC_%d_ADDR -- network address
#* CISCO_SPLIT_INC_%d_MASK -- subnet mask (for example: 255.255.255.0)
#* CISCO_SPLIT_INC_%d_MASKLEN -- subnet masklen (for example: 24)
#* CISCO_SPLIT_INC_%d_PROTOCOL -- protocol (often just 0)
#* CISCO_SPLIT_INC_%d_SPORT -- source port (often just 0)
#* CISCO_SPLIT_INC_%d_DPORT -- destination port (often just 0)
#* CISCO_IPV6_SPLIT_INC -- number of networks in IPv6 split-network-list
#* CISCO_IPV6_SPLIT_INC_%d_ADDR -- IPv6 network address
#* CISCO_IPV6_SPLIT_INC_$%d_MASKLEN -- IPv6 subnet masklen
if [ -z "$reason" ]; then
echo "this script must be called from vpnc" 1>&2
exit 1
fi
case "$reason" in
pre-init)
echo "PRE_INIT MARKER ***********************************"
route -n delete -host "$VPNGATEWAY"
;;
connect)
echo "CONNECT MARKER ***********************************"
route -n delete -host "$VPNGATEWAY"
ifconfig "$TUNDEV" inet "$INTERNAL_IP4_ADDRESS" "$INTERNAL_IP4_ADDRESS" netmask 255.255.255.255 mtu ${INTERNAL_IP4_MTU} up
route add -host "$VPNGATEWAY" "`netstat -nr -f inet | grep UHLWIi | xargs | cut -d " " -f 1`"
route add -net 128.0.0.0 "$INTERNAL_IP4_ADDRESS" -netmask 128.0.0.0
route add -net 0.0.0.0 "$INTERNAL_IP4_ADDRESS" -netmask 128.0.0.0
# route add -net "$INTERNAL_IP4_ADDRESS" "$INTERNAL_IP4_ADDRESS"
route add -net "$INTERNAL_IP4_NETADDR"/24 "$INTERNAL_IP4_ADDRESS"
networksetup -setdnsservers Wi-Fi $INTERNAL_IP4_DNS
;;
disconnect)
echo "DISCONNECT MARKER ***********************************"
route delete -host "$VPNGATEWAY"
route delete -net 128.0.0.0/1
route delete -net 0.0.0.0/1
# route delete "$INTERNAL_IP4_ADDRESS"
route delete "$INTERNAL_IP4_NETADDR"/24
networksetup -setdnsservers Wi-Fi empty
;;
reconnect)
echo "RECONNECT MARKER ***********************************"
# route -n delete -host "$VPNGATEWAY"
;;
*)
echo "unknown reason '$reason'. Maybe vpnc-script is out of date" 1>&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment