dotfiles/.config/eww/scripts/get-network.sh

62 lines
1.8 KiB
Bash
Raw Normal View History

#!/bin/bash
2024-11-05 14:09:13 +01:00
interval=2
while :; do
if_new=$(ip -j r list default | jq -r '.[0].dev')
unset type ip4 ip6 rate signal up down
if [[ "$if_new" != "null" ]]; then
if [[ "$if_new" != "$if" ]]; then
bytes_down=$(cat "/sys/class/net/$if_new/statistics/rx_bytes")
bytes_up=$(cat "/sys/class/net/$if_new/statistics/tx_bytes")
fi
type="$(nmcli -t -g general.type device show "$if_new")"
ip4="$(nmcli -t -g ip4.address device show "$if_new")"
ip6="$(nmcli -t -g ip6.address device show "$if_new" | sed 's/\\//g')"
name="${type^}"
if [[ "$type" == "wifi" ]]; then
icon="󰖩"
name="$(nmcli -t -g general.connection device show "$if_new")"
rate="$(nmcli -t -g 'AP' device show wlo1 | awk -F ':' '{if ($2 == "*") { print $12 } }')"
signal="$(nmcli -t -g 'AP' device show wlo1 | awk -F ':' '{if ($2 == "*") { print $13 } }')%"
elif [[ "$type" == "ethernet" ]]; then
icon="󰛳"
else
icon="󰲛"
fi
bytes_down_new=$(cat "/sys/class/net/$if_new/statistics/rx_bytes")
bitrate_down=$(( (bytes_down_new - bytes_down) / interval ))
bytes_down=$bytes_down_new
if [[ $bitrate_down -lt 1024 ]]; then
down="${bitrate_down}B/s"
else
down="$(numfmt --to=iec $bitrate_down)/s"
fi
bytes_up_new=$(cat "/sys/class/net/$if_new/statistics/tx_bytes")
bitrate_up=$(( (bytes_up_new - bytes_up) / interval ))
bytes_up=$bytes_up_new
if [[ $bitrate_up -lt 1024 ]]; then
up="${bitrate_up}B/s"
else
up="$(numfmt --to=iec $bitrate_up)/s"
fi
else
2024-11-05 14:09:13 +01:00
icon="󰲛"
name="Not connected"
fi
2024-11-05 14:09:13 +01:00
if="$if_new"
cat << EOF
{"if": "$if", "icon": "$icon", "type": "$type", "name": "$name", "ip4": "$ip4", "ip6": "$ip6", "rate": "$rate", "signal": "$signal", "down": "$down", "up": "$up"}
EOF
2024-11-05 14:09:13 +01:00
sleep $interval
done