cek-wg.sh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. echo > /etc/wireguard/clients.txt
  8. data=( `cat /etc/wireguard/wg0.conf | grep "### Client" | awk '{ print $3 }'`);
  9. hr(){
  10. numfmt --to=iec-i --suffix=B "$1"
  11. }
  12. x=1
  13. echo "-------------------------------";
  14. echo "---=[ Wireguard User Login ]=---";
  15. echo "-------------------------------";
  16. echo "Name Remote IP Virtual IP Bytes Received Bytes Sent Last Seen "
  17. for akun in "${data[@]}"
  18. do
  19. pub=$(cat /etc/wireguard/wg0.conf | grep PublicKey | awk '{ print $3 }' | tr '\n' ' ' | awk '{print $'"$x"'}')
  20. echo "$akun $pub" >> /etc/wireguard/clients.txt
  21. x=$(( "$x" + 1 ))
  22. done
  23. CLIENTS_FILE="/etc/wireguard/clients.txt"
  24. if [ ! -s "$CLIENTS_FILE" ]; then
  25. echo "::: There are no clients to list"
  26. exit 0
  27. fi
  28. listClients(){
  29. if DUMP="$(wg show wg0 dump)"; then
  30. DUMP="$(tail -n +2 <<< "$DUMP")"
  31. else
  32. exit 1
  33. fi
  34. printf "\e[1m::: Connected Clients List :::\e[0m\n"
  35. {
  36. printf "\e[4mName\e[0m \t \e[4mRemote IP\e[0m \t \e[4mVirtual IP\e[0m \t \e[4mBytes Received\e[0m \t \e[4mBytes Sent\e[0m \t \e[4mLast Seen\e[0m\n"
  37. while IFS= read -r LINE; do
  38. if [ -n "${LINE}" ]; then
  39. PUBLIC_KEY="$(awk '{ print $1 }' <<< "$LINE")"
  40. REMOTE_IP="$(awk '{ print $3 }' <<< "$LINE")"
  41. VIRTUAL_IP="$(awk '{ print $4 }' <<< "$LINE")"
  42. BYTES_RECEIVED="$(awk '{ print $6 }' <<< "$LINE")"
  43. BYTES_SENT="$(awk '{ print $7 }' <<< "$LINE")"
  44. LAST_SEEN="$(awk '{ print $5 }' <<< "$LINE")"
  45. CLIENT_NAME="$(grep "$PUBLIC_KEY" "$CLIENTS_FILE" | awk '{ print $1 }')"
  46. if [ "$HR" = 1 ]; then
  47. if [ "$LAST_SEEN" -ne 0 ]; then
  48. printf "%s \t %s \t %s \t %s \t %s \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$(hr "$BYTES_RECEIVED")" "$(hr "$BYTES_SENT")" "$(date -d @"$LAST_SEEN" '+%b %d %Y - %T')"
  49. else
  50. printf "%s \t %s \t %s \t %s \t %s \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$(hr "$BYTES_RECEIVED")" "$(hr "$BYTES_SENT")" "(not yet)"
  51. fi
  52. else
  53. if [ "$LAST_SEEN" -ne 0 ]; then
  54. printf "%s \t %s \t %s \t %'d \t %'d \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$BYTES_RECEIVED" "$BYTES_SENT" "$(date -d @"$LAST_SEEN" '+%b %d %Y - %T')"
  55. else
  56. printf "%s \t %s \t %s \t %'d \t %'d \t %s\n" "$CLIENT_NAME" "$REMOTE_IP" "${VIRTUAL_IP/\/32/}" "$BYTES_RECEIVED" "$BYTES_SENT" "(not yet)"
  57. fi
  58. fi
  59. fi
  60. done <<< "$DUMP"
  61. printf "\n"
  62. } | column -t -s $'\t'
  63. }
  64. listClients
  65. echo "-------------------------------";
  66. echo -e "By EvoTeamMalaysia"
  67. echo -e ""