Skip to content

Instantly share code, notes, and snippets.

@oz
Last active December 16, 2015 06:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oz/5392655 to your computer and use it in GitHub Desktop.
Save oz/5392655 to your computer and use it in GitHub Desktop.
This might come in handy whenever you need to add one or more hosts to a running VPN connection (on Mac OS).
#!/bin/sh
#
# vpnhost.sh - Add hosts/IP addresses to route through your VPN.
#
# - Routed hosts are not persisted between restarts of your VPN software.
# - The VPN's gateway route is hardcoded to be on the tun0 interface.
# - Use of sudo is hard-coded too.
# - Only works on OSX
VPN_IFACE=tun0
# Get a list of IP addresses from a hostname. IP addresses pass through
# untouched.
get_ip_addresses () {
# Cheap detection of an IP address...
if [ `echo "$1" | grep -nc -E '^(\d+.?){4}$'` -eq "1" ] ; then
echo "$1"
else
host "$1" | grep 'has address' | cut -d ' ' -f4
fi
}
# Add an IP address to the routing table through $vpn_iface
add_ip_to_vpn () {
echo "Adding VPN route for IP address $1... "
sudo route add -host $1 -interface $VPN_IFACE
}
if [ $# -lt 1 ] ; then
echo "Usage: $0 <IP Address|Hostname ...>"
exit 1
fi
for arg in $@; do
for ip_address in `get_ip_addresses "$arg"`; do
add_ip_to_vpn "$ip_address"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment