del-wg.sh 1.8 KB

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