Advertisement
Guest User

vpn kill switch linux

a guest
Jul 21st, 2017
4,888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.59 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  ################################################################################################################################
  4. ##                                                                                                                             ##
  5. ## This script kills the internet connectivity should the VPN connection, once established, drop.                              ##
  6. ## To enable networking again using the terminal, use: $ nmcli networking on                                                   ##
  7. ##                                                                                                                             ##
  8. ## This script relies on the package 'network-manager' to disable the network, and on 'libnotify-bin' to send notifications.   ##
  9. ## This script does *not* need root permissions (in common system configurations).                                             ##
  10. ##                                                                                                                             ##
  11. ## This script was written for and tested on Debian 9 Stretch, but should work on most Debian derivates.                       ##
  12. ## It comes without any warranty and you use it on your own risk - you alone are responsible for your actions on the Internet. ##
  13. ##                                                                                                                             ##
  14. ## Other than that, you can do whatever you like with this script. Enjoy!                                                      ##
  15. ##                                                                                                                             ##
  16. ################################################################################################################################
  17.  
  18.  
  19. INTERFACE="tun0"            ## default tun0 - the vpn interface that will be monitored.
  20. VPN_EXISTS_CHECK="30"       ## default 30   - the time to wait between scans for an active VPN connection
  21. VPN_DROPPED_CHECK="0.25"    ## default 0.25 - the time to wait for the next check whether the VPN interface still exists
  22.  
  23. ECHO="0"                    ## set to "1" if you want/need terminal output.
  24.  
  25.  
  26. ## the script will run in an infinite loop.
  27. while :
  28. do
  29.  
  30.     ## first, the script will check if there's a VPN connection to be monitored.
  31.     if ! [[ $(ip addr | grep $INTERFACE) = "" ]]; then
  32.  
  33.         ## if $INTERFACE exists, there is a VPN connection.
  34.         notify-send "VPN connection was found and will be monitored."
  35.         if [[ $ECHO == "1" ]]; then echo "[$(date +%H:%M:%S)]: VPN found; monitoring."; fi
  36.  
  37.         ## if a VPN connection was detected, it will be watched using an infinite loop.
  38.         while :
  39.         do
  40.             ## let's check if the tun0 interface is still there...
  41.             if [[ $(ip addr | grep $INTERFACE) = "" ]]; then
  42.  
  43.                 ## Oops! the $INTERFACE interface vanished, quickly kill networking and notify the user.
  44.                 nmcli networking off
  45.                 notify-send "VPN interface vanished; Network connectivity has been disabled."
  46.                 if [[ $ECHO == "1" ]]; then echo "[$(date +%H:%M:%S)]: VPN died; networking disabled."; fi
  47.  
  48.                 ## As the network connection was killed, we can stop looking if the interface still exists
  49.                 break
  50.             fi
  51.  
  52.             ## if $INTERFACE is still there... check again after the time specified in $VPN_DROPPED_CHECK passed.
  53.             if [[ $ECHO == "1" ]]; then echo "[$(date +%H:%M:%S)]: VPN is ok."; fi
  54.             sleep $VPN_DROPPED_CHECK
  55.         done
  56.  
  57.     fi
  58.  
  59.     ## if the script reaches this point, there's no VPN connection at the time.
  60.     ## the script will wait the time set in $VPN_EXISTS_CHECK for the next check.
  61.     sleep $VPN_EXISTS_CHECK
  62.  
  63. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement