2024-09-20 18:45:02 +02:00
|
|
|
#!/usr/bin/env dash
|
2024-01-25 14:39:08 +01:00
|
|
|
|
2024-09-22 10:56:04 +02:00
|
|
|
|
|
|
|
print_network_state() {
|
2024-06-12 19:45:58 +02:00
|
|
|
case "$1" in
|
2024-10-18 09:25:12 +02:00
|
|
|
eth*|enp*)
|
2024-06-12 19:45:58 +02:00
|
|
|
echo ethernet
|
|
|
|
;;
|
2024-10-18 09:25:12 +02:00
|
|
|
wlan*|wlp*)
|
2024-10-12 19:23:25 +02:00
|
|
|
echo wireless
|
|
|
|
;;
|
2024-06-12 19:45:58 +02:00
|
|
|
usb*)
|
|
|
|
echo tethering
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo disconnected
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
wifi_strength() {
|
2024-10-12 19:28:37 +02:00
|
|
|
if [ "$1" -le 20 ]; then echo terrible
|
|
|
|
elif [ "$1" -le 40 ]; then echo bad
|
|
|
|
elif [ "$1" -le 60 ]; then echo mediocre
|
|
|
|
elif [ "$1" -le 80 ]; then echo good
|
|
|
|
else echo excellent
|
2024-06-12 19:45:58 +02:00
|
|
|
fi
|
|
|
|
}
|
2024-01-25 14:39:08 +01:00
|
|
|
|
2024-09-22 10:56:04 +02:00
|
|
|
print_network_infos() {
|
2024-06-12 19:45:58 +02:00
|
|
|
if [ -n "$1" ]; then
|
2024-10-15 09:29:21 +02:00
|
|
|
device="$1"
|
|
|
|
route_line=$(ip route show dev "$device" | awk '($1 == "default") { print }')
|
2024-01-25 14:39:08 +01:00
|
|
|
|
2024-06-12 19:45:58 +02:00
|
|
|
printf ',"ip":{"local":"%s","gateway":"%s"}' \
|
|
|
|
"$(echo "$route_line" | awk '{ print $7 }')" \
|
|
|
|
"$(echo "$route_line" | awk '{ print $3 }')"
|
|
|
|
|
|
|
|
if [ "$state" = wireless ]; then
|
2024-10-17 09:47:56 +02:00
|
|
|
wireless_info=$(nmcli --get-values active,ssid,signal dev wifi list --rescan no | grep "^yes")
|
|
|
|
ssid=$(echo "$wireless_info" | cut -d : -f2)
|
|
|
|
signal=$(echo "$wireless_info" | cut -d : -f3)
|
2024-06-12 19:45:58 +02:00
|
|
|
printf ',"wifi":{"signal":"%s","ssid":"%s"}' \
|
|
|
|
"$(wifi_strength "$signal")" \
|
2024-10-15 09:29:21 +02:00
|
|
|
"$ssid"
|
2024-06-12 19:45:58 +02:00
|
|
|
fi
|
2024-01-25 14:39:08 +01:00
|
|
|
fi
|
2024-06-12 19:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
print_network_status() {
|
|
|
|
device=$(ip route | awk '($1 == "default") { print $5 }')
|
2024-09-22 10:56:04 +02:00
|
|
|
state=$(print_network_state "$device")
|
2024-01-25 14:39:08 +01:00
|
|
|
|
2024-06-12 19:45:58 +02:00
|
|
|
printf '{"state":"%s"%s}\n' \
|
|
|
|
"$state" \
|
2024-09-22 10:56:04 +02:00
|
|
|
"$(print_network_infos "$device")"
|
|
|
|
}
|
|
|
|
|
|
|
|
print_bluetooth_status (){
|
|
|
|
power=$(bluetoothctl show | grep Powered | awk '{print $2}' | sed 's/yes/on/g; s/no/off/g')
|
|
|
|
count=$(bluetoothctl devices Connected | wc -l)
|
|
|
|
echo "{\"power\": \"${power}\", \"count\": \"${count}\"}"
|
|
|
|
}
|
|
|
|
|
|
|
|
print_connectivity_info () {
|
|
|
|
network="$(print_network_status)"
|
|
|
|
bluetooth="$(print_bluetooth_status)"
|
|
|
|
|
|
|
|
echo "{\"bluetooth\": ${bluetooth}, \"network\": ${network}}"
|
2024-01-25 14:39:08 +01:00
|
|
|
}
|
|
|
|
|
2024-09-22 10:56:04 +02:00
|
|
|
print_connectivity_info
|
|
|
|
dbus-monitor --system "interface=org.freedesktop.DBus.ObjectManager" 2> /dev/null | while read -r line; do
|
|
|
|
print_connectivity_info
|
2024-11-14 09:15:25 +01:00
|
|
|
sleep 0.1
|
2024-01-25 14:39:08 +01:00
|
|
|
done
|