Skip to content

Instantly share code, notes, and snippets.

@markosamuli
Created February 5, 2014 16:46
Show Gist options
  • Save markosamuli/8827942 to your computer and use it in GitHub Desktop.
Save markosamuli/8827942 to your computer and use it in GitHub Desktop.
vpn-route-add
#!/bin/bash
SCRIPT=$(basename $0)
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
TARGET_HOST=$1
if [ -z "$TARGET_HOST" ]; then
echo "Target hostname missing"
echo "Usage: $SCRIPT <hostname>"
exit 1
fi
# VPN interface (update this)
VPN_IF=gpd0
VPN_IP=$(ifconfig | grep $VPN_IF -A2 | grep "inet" | awk '{print $2}')
if [ -z "$VPN_IP" ]; then
echo "Failed to resolve VPN gateway IP address"
exit 1
fi
# Get all IP addresses for this hostname
TARGET_IPS=$(host $TARGET_HOST | grep "has address" | awk '{print $4}' | xargs)
# Add routes to all of these addresses
echo "VPN gateway IP: $VPN_IP"
for IP in $TARGET_IPS; do
route add $IP $VPN_IP
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment