Last active
June 19, 2024 17:59
-
-
Save fvdm/cb5b90541b076914ee897d048cd4f549 to your computer and use it in GitHub Desktop.
Bash script to list stats for connected WireGuard clients
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Human readable statistics for connected WireGuard clients | |
| # | |
| # Source: https://gist.github.com/fvdm/cb5b90541b076914ee897d048cd4f549 | |
| # | |
| # This script is following the 'wireguard-manager' storage | |
| # Filenames: [wg_iface]-client-[name].conf | |
| # | |
| # Set 'wg_clients' to the path where client configs are saved | |
| # Set 'wg_iface' to the WireGuard network interface | |
| # or provide it in the first argument | |
| wg_clients="/root/wireguard/clients/" | |
| wg_iface="wg0" | |
| if [ "$1" != "" ]; then | |
| wg_iface=$1 | |
| fi | |
| declare -g sumrx=0 | |
| declare -g sumtx=0 | |
| function human () { | |
| echo -e "$1" \ | |
| | numfmt --to=iec --padding=4 \ | |
| | sed -r 's/([0-9.]+)([A-Z])$/\\e[0;1m\1\\e[93m\2\\e[0m/' | |
| } | |
| echo -e "\e[93;1m" | |
| echo "Latest handshake RX TX Client name Endpoint" | |
| echo "------------------- ---- ---- --------------- --------" | |
| echo -ne "\e[0;0m" | |
| while read line; do | |
| ips=$(echo -e "$line" | cut -f4) | |
| name=$( \ | |
| sudo find $wg_clients \ | |
| -name $wg_iface'-*.conf' \ | |
| -exec grep -l "$ips" {} \; \ | |
| | sed -r 's/.+\/'$wg_iface'-client-//' \ | |
| | sed -r 's/\.conf$//' \ | |
| ) | |
| name=$(printf "%-15s" $name) | |
| endpoint=$(echo -e "$line" | cut -f3 | sed -r 's/:[0-9]+$//') | |
| latest=$(echo -e "$line" | cut -f5) | |
| latest=$(date -d @$latest +'%F %T') | |
| rx=$(echo -e "$line" | cut -f6) | |
| sumrx=$(($sumrx+$rx)) | |
| rx=$(human $rx) | |
| tx=$(echo -e "$line" | cut -f7) | |
| sumtx=$(($sumtx+$tx)) | |
| tx=$(human $tx) | |
| echo -e "$latest $rx $tx $name $endpoint" | |
| done < <( \ | |
| sudo wg show $wg_iface dump \ | |
| | tail -n +2 \ | |
| | grep -v '(none)' \ | |
| ) | |
| sumrx=$(human $sumrx) | |
| sumtx=$(human $sumtx) | |
| echo -ne "\e[93;1m" | |
| echo -e " ---- ----" | |
| echo -e " $sumrx $sumtx" | |
| echo -e "\e[0;0m" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment