75 lines
1.9 KiB
Bash
Executable file
75 lines
1.9 KiB
Bash
Executable file
#!/usr/bin/env dash
|
|
|
|
|
|
print_network_state() {
|
|
case "$1" in
|
|
eth*|enp*)
|
|
echo ethernet
|
|
;;
|
|
wlan*|wlp*)
|
|
echo wireless
|
|
;;
|
|
usb*)
|
|
echo tethering
|
|
;;
|
|
*)
|
|
echo disconnected
|
|
;;
|
|
esac
|
|
}
|
|
|
|
wifi_strength() {
|
|
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
|
|
fi
|
|
}
|
|
|
|
print_network_infos() {
|
|
if [ -n "$1" ]; then
|
|
device="$1"
|
|
route_line=$(ip route show dev "$device" | awk '($1 == "default") { print }')
|
|
|
|
printf ',"ip":{"local":"%s","gateway":"%s"}' \
|
|
"$(echo "$route_line" | awk '{ print $7 }')" \
|
|
"$(echo "$route_line" | awk '{ print $3 }')"
|
|
|
|
if [ "$state" = wireless ]; then
|
|
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)
|
|
printf ',"wifi":{"signal":"%s","ssid":"%s"}' \
|
|
"$(wifi_strength "$signal")" \
|
|
"$ssid"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
print_network_status() {
|
|
device=$(ip route | awk '($1 == "default") { print $5 }')
|
|
state=$(print_network_state "$device")
|
|
|
|
printf '{"state":"%s"%s}\n' \
|
|
"$state" \
|
|
"$(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}}"
|
|
}
|
|
|
|
print_connectivity_info
|
|
dbus-monitor --system "interface=org.freedesktop.DBus.ObjectManager" 2> /dev/null | while read -r line; do
|
|
print_connectivity_info
|
|
done
|