Skip to content

Instantly share code, notes, and snippets.

@eddieoz
Forked from intrd/vpn_intrd.sh
Created September 24, 2025 16:20
Show Gist options
  • Save eddieoz/8cb1bf34ae99a998315279185635f63b to your computer and use it in GitHub Desktop.
Save eddieoz/8cb1bf34ae99a998315279185635f63b 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