del-wg.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/bin/bash
  2. source /etc/wireguard/params
  3. NUMBER_OF_CLIENTS=$(grep -c -E "^### Client" "/etc/wireguard/$SERVER_WG_NIC.conf")
  4. if [[ ${NUMBER_OF_CLIENTS} == '0' ]]; then
  5. clear
  6. echo ""
  7. echo "You have no existing clients!"
  8. exit 1
  9. fi
  10. clear
  11. echo ""
  12. echo ""
  13. echo " Select the existing client you want to remove"
  14. echo " Press CTRL+C to return"
  15. echo " ==============================="
  16. echo " No Expired User"
  17. grep -E "^### Client" "/etc/wireguard/$SERVER_WG_NIC.conf" | cut -d ' ' -f 3-4 | nl -s ') '
  18. until [[ ${CLIENT_NUMBER} -ge 1 && ${CLIENT_NUMBER} -le ${NUMBER_OF_CLIENTS} ]]; do
  19. if [[ ${CLIENT_NUMBER} == '1' ]]; then
  20. read -rp "Select one client [1]: " CLIENT_NUMBER
  21. else
  22. read -rp "Select one client [1-${NUMBER_OF_CLIENTS}]: " CLIENT_NUMBER
  23. fi
  24. done
  25. # match the selected number to a client name
  26. CLIENT_NAME=$(grep -E "^### Client" "/etc/wireguard/$SERVER_WG_NIC.conf" | cut -d ' ' -f 3-4 | sed -n "${CLIENT_NUMBER}"p)
  27. user=$(grep -E "^### Client" "/etc/wireguard/$SERVER_WG_NIC.conf" | cut -d ' ' -f 3 | sed -n "${CLIENT_NUMBER}"p)
  28. exp=$(grep -E "^### Client" "/etc/wireguard/$SERVER_WG_NIC.conf" | cut -d ' ' -f 4 | sed -n "${CLIENT_NUMBER}"p)
  29. # remove [Peer] block matching $CLIENT_NAME
  30. sed -i "/^### Client $user $exp/,/^AllowedIPs/d" /etc/wireguard/wg0.conf
  31. # remove generated client file
  32. rm -f "/home/vps/public_html/$user.conf"
  33. # restart wireguard to apply changes
  34. systemctl restart "wg-quick@$SERVER_WG_NIC"
  35. service cron restart
  36. clear
  37. echo " Wireguard Account Deleted Successfully"
  38. echo " =========================="
  39. echo " Client Name : $user"
  40. echo " Expired On : $exp"
  41. echo " =========================="