renew-wg.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 "Select an existing client that you want to renew"
  13. echo " Press CTRL+C to return"
  14. echo -e "==============================="
  15. echo " No Expired User"
  16. grep -E "^### Client" "/etc/wireguard/$SERVER_WG_NIC.conf" | cut -d ' ' -f 3-4 | nl -s ') '
  17. until [[ ${CLIENT_NUMBER} -ge 1 && ${CLIENT_NUMBER} -le ${NUMBER_OF_CLIENTS} ]]; do
  18. if [[ ${CLIENT_NUMBER} == '1' ]]; then
  19. read -rp "Select one client [1]: " CLIENT_NUMBER
  20. else
  21. read -rp "Select one client [1-${NUMBER_OF_CLIENTS}]: " CLIENT_NUMBER
  22. fi
  23. done
  24. read -p "Expired (days): " masaaktif
  25. user=$(grep -E "^### Client" "/etc/wireguard/wg0.conf" | cut -d ' ' -f 3 | sed -n "${CLIENT_NUMBER}"p)
  26. exp=$(grep -E "^### Client" "/etc/wireguard/wg0.conf" | cut -d ' ' -f 4 | sed -n "${CLIENT_NUMBER}"p)
  27. now=$(date +%Y-%m-%d)
  28. d1=$(date -d "$exp" +%s)
  29. d2=$(date -d "$now" +%s)
  30. exp2=$(( (d1 - d2) / 86400 ))
  31. exp3=$(($exp2 + $masaaktif))
  32. exp4=`date -d "$exp3 days" +"%Y-%m-%d"`
  33. sed -i "s/### Client $user $exp/### Client $user $exp4/g" /etc/wireguard/wg0.conf
  34. clear
  35. echo ""
  36. echo " Wireguard Account Has Been Successfully Renewed"
  37. echo " =========================="
  38. echo " Client Name : $user"
  39. echo " Expired On: $exp4"
  40. echo " =========================="