Skip to content

Instantly share code, notes, and snippets.

@andresvia
Last active January 5, 2017 23:46
Show Gist options
  • Save andresvia/d3699ea86e4a5dce62c8480c3cc71fcb to your computer and use it in GitHub Desktop.
Save andresvia/d3699ea86e4a5dce62c8480c3cc71fcb to your computer and use it in GitHub Desktop.
tunnelme - Split tunnel connections to a specific host or IP over a certain gateway

Situation:

Your VPN does not automatically set split tunnel for your connection to some host, maybe the ipaddress of the host changes over time, like an AWS ELB, and network admins can't set split tunnel rules on changing addresses.

Requirements:

User must be able to run:

sudo route add -host "$dst" "$tunnel"

How to use:

  1. Download tunnelme.sh

  2. chmod +x tunnelme.sh

  3. Run script

./tunnelme.sh <host|ipaddress>
     1	 133 x.y.z.f utun1
     2	   3 link#16 utun1
What is your tunnel, (use number): 1
add host host|ipaddress.: gateway x.y.z.f
add host ipaddress: gateway x.y.z.f
#!/bin/bash -eu
dst="$1"
netstat -nr | awk '$6~/tun/{print $2, $6}' | sort | uniq -c | sort -nr > /tmp/tunnelme
cat -n /tmp/tunnelme
read -p "What is your tunnel, (use number): " n
tunnel="$(awk 'NR=='"$n"'{print $2}' /tmp/tunnelme)"
if [ "$(dig +short "$dst")" = "" ]
then
sudo route add -host "$dst" "$tunnel"
else
for idst in $(dig +short "$dst")
do
sudo route add -host "$idst" "$tunnel"
done
fi
@andresvia
Copy link
Author

TODO: add support for Linux.

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