Skip to content

Instantly share code, notes, and snippets.

@intrd
Last active January 29, 2025 10:05
Show Gist options
  • 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: [email protected]
# 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 routes
#!/bin/bash
## Restore from Openvpn safe kill switch
# Author: [email protected]
sudo route add -net 10.100.55.0 netmask 255.255.255.0 dev eth0
sudo route add default gw 10.100.55.1 eth0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment