pipidodo

VPN abort script

Jun 18th, 2021 (edited)
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.68 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. interface="org.freedesktop.NetworkManager.VPN.Connection"
  4. member="VpnStateChanged"
  5. logPath="/tmp/vpndemon"
  6. header="VPNDemon\nhttps://github.com/primaryobjects/vpndemon\n\n"
  7.  
  8. # Clear log file.
  9. > "$logPath"
  10.  
  11. list_descendants()
  12. {
  13.     local children=$(ps -o pid= --ppid "$1")
  14.  
  15.     for pid in $children
  16.     do
  17.         list_descendants "$pid"
  18.     done
  19.  
  20.     echo "$children"
  21. }
  22.  
  23. # Consider the first argument as the target process
  24. killProgramName="$1"
  25. if [ -z "$killProgramName" ]
  26. then
  27.     killProgramName=$(zenity --entry --title="VPNDemon" --text="$header Enter name of process to kill when VPN disconnects:")
  28. fi
  29.  
  30. result=$?
  31. if [ $result = 0 ]
  32. then
  33.     if [ $killProgramName ]
  34.     then
  35.         header="$header Target: $killProgramName\n\n"
  36.  
  37.         (tail -f "$logPath") |
  38.         {
  39.             zenity --progress --title="VPNDemon" --text="$header Monitoring VPN" --pulsate
  40.  
  41.             # Kill all child processes upon exit.
  42.             kill $(list_descendants $$)
  43.         } |
  44.         {
  45.             # Monitor for VPNStateChanged event.
  46.             dbus-monitor --system "type='signal',interface='$interface',member='$member'" |
  47.             {
  48.                 # Read output from dbus.
  49.                 (while read -r line
  50.                 do
  51.                     currentDate=`date +'%m-%d-%Y %r'`
  52.  
  53.                     # Check if this a VPN connection (uint32 2) event.
  54.                     if [ x"$(echo "$line" | grep 'uint32 3')" != x ]
  55.                     then
  56.                         echo "VPN Connected $currentDate"
  57.                         echo "# $header VPN Connected $currentDate" >> "$logPath"
  58.                     fi
  59.  
  60.                     # Check if this a VPN disconnected (uint32 7) event.
  61.                     if [ x"$(echo "$line" | grep 'uint32 6\|uint32 7')" != x ]
  62.                     then
  63.                         echo "VPN Disconnected $currentDate"
  64.                         echo "# $header VPN Disconnected $currentDate" >> "$logPath"
  65.  
  66.                         # Kill target process.
  67.                         for i in `pgrep $killProgramName`
  68.                         do
  69.                             # Get process name.
  70.                             name=$(ps -ocommand= -p $i | awk -F/ '{print $NF}' | awk '{print $1}')
  71.  
  72.                             # Kill process.
  73.                             kill $i
  74.  
  75.                             # Log result.
  76.                             echo "Terminated $i ($name)"
  77.                             echo "Terminated $i ($name)" >> "$logPath"
  78.                         done
  79.                     fi
  80.                 done)
  81.             }
  82.         }
  83.     else
  84.         zenity --error --text="No process name entered."
  85.     fi
  86. fi
  87.  
Add Comment
Please, Sign In to add comment