61 lines
1.8 KiB
Bash
Executable file
61 lines
1.8 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
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
|
|
icon=""
|
|
name="Not connected"
|
|
fi
|
|
|
|
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
|
|
|
|
sleep $interval
|
|
done
|