cek-wg.sh 2.7 KB

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