Files
tailscale-tools/exitnode-bw.sh
alf 5a8379a30b Fix: Globale Variable n in matches() überschrieb Zähler
- matches() verwendete n für split(), was den globalen Zähler
  im Hauptblock überschrieb
- Daten wurden bei Index 3 gespeichert, aber Schleife lief 1..2
- Lösung: Lokale Variablen in matches() deklariert (cnt, i, arr)
- Tab als Trennzeichen für join (Hostnamen mit Leerzeichen)
- korrekte awk-Feldindizes nach join

Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-10-28 12:57:53 +01:00

92 lines
3.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# exitnode-bw.sh – Exit-Node-Bandbreite für festgelegte Geräte
# Benötigt: tailscale, jq, awk, gum
#
# Zeigt die Up-/Download-Bandbreite ausgewählter Geräte an,
# die über einen Tailscale Exit-Node verbunden sind.
INTERVAL=5
BARWIDTH=40
MIN_BAR=1 # Mindest-Balkenlänge bei Aktivität (>0)
DEVICES=("PC77" "S22") # Partial-Match, case-insensitive
A=$(mktemp); B=$(mktemp)
trap 'rm -f "$A" "$B"' EXIT
# Erstelle Pipe-separierte Liste für partial match
DEVLIST=$(printf '%s|' "${DEVICES[@]}")
DEVLIST=${DEVLIST%|} # trailing | entfernen
# Tab als Trennzeichen verwenden (Hostnamen können Leerzeichen enthalten!)
snap() {
tailscale status --json | jq -r '.Peer[] | "\(.HostName)\t\(.TxBytes)\t\(.RxBytes)\t\(.Online)"' | sort
}
snap > "$A"
while sleep "$INTERVAL"; do
snap > "$B"
clear
gum style --bold --foreground 212 "⇅ Exit-Node Traffic $(date +%H:%M:%S)"
echo ""
# join mit Tab als Trennzeichen: $1=Host, $2=Tx_alt, $3=Rx_alt, $4=Online_alt, $5=Tx_neu, $6=Rx_neu, $7=Online_neu
join -t $'\t' "$A" "$B" | awk -F'\t' -v iv="$INTERVAL" -v w="$BARWIDTH" -v minbar="$MIN_BAR" \
-v devs="$DEVLIST" -v IGNORECASE=1 '
function mbit(kbits) {
return sprintf("%6.2f Mbit/s", kbits/1e3)
}
function bar(rate, max, s, n, i) {
if (rate <= 0) return ""
n = (max > 0) ? int(rate / max * w) : w
if (n < minbar) n = minbar # Mindest-Balken bei Aktivität
if (n > w) n = w
for (i = 0; i < n; i++) s = s "█"
return s
}
function pad(str, width, s, i) {
s = str
for (i = length(str); i < width; i++) s = s " "
return s
}
# Partial match: prüfe ob Hostname eines der Geräte enthält
# WICHTIG: cnt statt n verwenden, da n global ist und im Hauptblock gebraucht wird!
function matches(host, cnt, i, arr) {
cnt = split(devs, arr, "|")
for (i = 1; i <= cnt; i++) {
if (index(tolower(host), tolower(arr[i])) > 0) return 1
}
return 0
}
matches($1) {
dt = ($5 > $2) ? $5 - $2 : 0 # Tx_neu - Tx_alt
dr = ($6 > $3) ? $6 - $3 : 0 # Rx_neu - Rx_alt
online = ($7 == "true") ? 1 : 0 # Online-Status aus neuer Messung
n++
host[n] = $1
u[n] = dt * 8 / iv / 1e3 # kbit/s
d[n] = dr * 8 / iv / 1e3 # kbit/s
ol[n] = online
if (u[n] > mx) mx = u[n]
if (d[n] > mx) mx = d[n]
if (mx == 0) mx = 1 # Verhindere Division durch 0
}
END {
if (n == 0) {
print "\033[90m (keines der Geräte gefunden)\033[0m"
print "\033[90m Geprüfte Namen: " devs "\033[0m"
}
for (i = 1; i <= n; i++) {
ubar = bar(u[i], mx)
dbar = bar(d[i], mx)
upad = pad(ubar, w)
dpad = pad(dbar, w)
status = ol[i] ? "\033[32m●\033[0m" : "\033[90m○\033[0m"
printf "%s %-18s \033[36m↑\033[0m \033[36m%s\033[0m %s \033[32m↓\033[0m \033[32m%s\033[0m %s\n",
status, host[i], upad, mbit(u[i]), dpad, mbit(d[i])
}
}'
echo ""
gum style --foreground 240 " Interval: ${INTERVAL}s | Geräte: ${DEVICES[*]} | Strg+C zum Beenden"
mv "$B" "$A"
done