#!/bin/sh # Misst beide Uplinks direkt (am Tunnel vorbei) und meldet das Ergebnis an Home Assistant Arcen. # Verbraucht ca. 2 x 1 MB ueber Landal und 2 x 1 MB ueber den Huawei. HA_HOOK=$(sed -n 's/^HA_HOOK=\(.*\)/\1/p' /usr/sbin/uplink-watchdog | head -1) URL="http://speed.cloudflare.com/__down?bytes=1000000" mess() { # $1 = Geraet; Ausgabe: mittlere kB/s und Ping ms d=$1; sum=0; n=0 for i in 1 2; do s=$(curl -s -m 20 --interface $d -o /dev/null -w '%{speed_download}' "$URL" 2>/dev/null); s=${s%%.*}; [ -z "$s" ] && s=0 sum=$((sum + s)); n=$((n + 1)) done kb=$((sum / n / 1000)) p=$(ping -c 4 -W 2 -I $d 1.1.1.1 2>/dev/null | sed -n 's/.*= [0-9.]*\/\([0-9.]*\)\/.*/\1/p') echo "$kb ${p%%.*}" } set -- $(mess phy1-sta0); L_KB=$1; L_MS=${2:-0} set -- $(mess phy0-sta0); H_KB=$1; H_MS=${2:-0} PREV=$(sed -n 's/^bevorzugt=//p' /tmp/uplink-watchdog.state 2>/dev/null | head -1) # wer deutlich (10 %) schneller ist, wird bevorzugt; sonst bleibt es wie es ist if [ "$L_KB" -ge $((H_KB * 11 / 10)) ]; then P=landal elif [ "$H_KB" -ge $((L_KB * 11 / 10)) ]; then P=huawei else P=${PREV:-landal}; fi N="Test $(date +%H:%M): Landal $((L_KB * 8 / 1000)) Mbit/s, Ping ${L_MS} ms, Huawei $((H_KB * 8 / 1000)) Mbit/s, Ping ${H_MS} ms" echo "$(date '+%d.%m.%Y %H:%M:%S') $N -> bevorzugt $P" > /tmp/uplink-test.result logger -t uplink-test "$N -> bevorzugt $P" # der uplink-watchdog uebernimmt das Ergebnis und meldet es an Home Assistant printf 'T_PREF=%s\nT_H=%s\nT_L=%s\nT_NOTE="%s"\n' "$P" "$H_KB" "$L_KB" "$N" > /tmp/uplink-test.pref