pointfichiers/bar/eww/scripts/get-connectivity

60 lines
1.3 KiB
Text
Raw Normal View History

2024-09-20 18:45:02 +02:00
#!/usr/bin/env dash
2024-01-25 14:39:08 +01:00
print_state() {
case "$1" in
eth*)
echo ethernet
;;
wlan*)
echo wireless
;;
usb*)
echo tethering
;;
*)
echo disconnected
;;
esac
}
wifi_strength() {
if [ "$1" -le -80 ]; then echo terrible
elif [ "$1" -le -70 ]; then echo bad
elif [ "$1" -le -60 ]; then echo mediocre
elif [ "$1" -le -40 ]; then echo good
else echo excellent
fi
}
2024-01-25 14:39:08 +01:00
print_infos() {
if [ -n "$1" ]; then
route_line=$(ip route show dev "$1" | awk '($1 == "default") { print }')
2024-01-25 14:39:08 +01:00
printf ',"ip":{"local":"%s","gateway":"%s"}' \
"$(echo "$route_line" | awk '{ print $7 }')" \
"$(echo "$route_line" | awk '{ print $3 }')"
if [ "$state" = wireless ]; then
signal=$(iw dev "$1" link | awk '($1 == "signal:") { print $2}')
printf ',"wifi":{"signal":"%s","ssid":"%s"}' \
"$(wifi_strength "$signal")" \
"$(iw dev wlan0 info | grep '^\s*ssid ' | xargs | cut -d \ -f 2-)"
fi
2024-01-25 14:39:08 +01:00
fi
}
print_network_status() {
device=$(ip route | awk '($1 == "default") { print $5 }')
state=$(print_state "$device")
2024-01-25 14:39:08 +01:00
printf '{"state":"%s"%s}\n' \
"$state" \
"$(print_infos "$device")"
2024-01-25 14:39:08 +01:00
}
print_network_status
tail -f -n 0 /run/dhcpcd/log /var/log/iwd/current | \
while read -r _unused; do
print_network_status
2024-01-25 14:39:08 +01:00
done