Files
tailscale-tools/exitnode-bw.sh
2026-10-28 12:22:14 +00:00

117 lines
4.3 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.
# Berücksichtigt sowohl Peers als auch den lokalen Node (Self).
INTERVAL=5
BARWIDTH=40
MIN_BAR=1 # Mindest-Balkenlänge bei Aktivität (>0)
MIN_SCALE=25 # Mindest-Skala in Mbit/s für vollen Balken
# (verhindert Vollausschlag bei <1 Mbit/s)
INCLUDE_SELF=true # Auch den lokalen Computer anzeigen?
DEVICES=("PC7-77" "S22") # Partial-Match, case-insensitive
# PC7-77 = lokal (Self), S22 = Handy (Peer)
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() {
local json
json=$(tailscale status --json)
{
# Self (lokaler Computer) – falls gewünscht
if [ "$INCLUDE_SELF" = "true" ]; then
echo "$json" | jq -r '.Self | "\(.HostName)\t\(.TxBytes)\t\(.RxBytes)\t\(.Online)\tself"'
fi
# Peers (alle anderen Geräte)
echo "$json" | jq -r '.Peer[] | "\(.HostName)\t\(.TxBytes)\t\(.RxBytes)\t\(.Online)\tpeer"'
} | 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: $1=Host, $2=Tx_alt, $3=Rx_alt, $4=Online_alt, $5=Type_alt,
# $6=Tx_neu, $7=Rx_neu, $8=Online_neu, $9=Type_neu
join -t $'\t' "$A" "$B" | awk -F'\t' -v iv="$INTERVAL" -v w="$BARWIDTH" -v minbar="$MIN_BAR" \
-v minscale="$MIN_SCALE" -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: Lokale Variablen deklarieren (cnt, i, arr), da n global ist!
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 = ($6 > $2) ? $6 - $2 : 0 # Tx_neu - Tx_alt
dr = ($7 > $3) ? $7 - $3 : 0 # Rx_neu - Rx_alt
online = ($8 == "true") ? 1 : 0 # Online-Status aus neuer Messung
type = $9 # "self" oder "peer"
n++
host[n] = $1
u[n] = dt * 8 / iv / 1e3 # kbit/s
d[n] = dr * 8 / iv / 1e3 # kbit/s
ol[n] = online
ty[n] = type
if (u[n] > mx) mx = u[n]
if (d[n] > mx) mx = d[n]
if (mx == 0) mx = 1 # Verhindere Division durch 0
}
END {
# Mindest-Skala anwenden: Voller Balken erst ab MIN_SCALE Mbit/s
if (mx < minscale) mx = minscale
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-Symbol: ● online, ○ offline, ★ self (lokaler Computer)
if (ty[i] == "self")
status = "\033[33m★\033[0m" # gelb für lokalen Computer
else if (ol[i])
status = "\033[32m●\033[0m" # grün für online Peer
else
status = "\033[90m○\033[0m" # grau für offline Peer
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[*]} | ★=lokal | Strg+C zum Beenden"
mv "$B" "$A"
done