Skip to content

Instantly share code, notes, and snippets.

@intrd
Last active June 14, 2022 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intrd/4f252f4c8dad0db0e2186859d899070e to your computer and use it in GitHub Desktop.
Save intrd/4f252f4c8dad0db0e2186859d899070e to your computer and use it in GitHub Desktop.
Openvpn safe kill switch / isolate vpn connection using linux routing table (no iptables needed)
#!/bin/bash
## Openvpn safe kill switch / isolate vpn connection using linux routing table (no iptables needed)
# Author: intrd@dann.com.br
# flush the entire routing table (incl cache)
sudo ip route flush table main
sudo ip route flush cache
# route the wan network but not a gateway
sudo route add -net 10.100.55.0 netmask 255.255.255.0 dev eth0
# shuffle and choose a rand ovpn file
VPNFILE=$(find /vpn/ -name *.ovpn|shuf|head -n1)
# add a route for all remote ips found in ovpn files pointing to wan gateway
cat $VPNFILE | grep -P "remote \d"|cut -d" " -f2|sort -u | while read ip; do sudo route add $ip gw 10.100.55.1 eth0; done
# run ovpn w/ provided creds
sudo openvpn --config $VPNFILE --auth-user-pass /vpn/cred.txt
## vpn_restore.sh - to restore (if needed)
# sudo route add -net 10.100.55.0 netmask 255.255.255.0 dev eth0
# sudo route add default gw <YOURLOCALGW> eth0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment