#!/bin/sh . libsb CACHE="${XDG_RUNTIME_DIR}/nettrafcache" get_physical_devices() { nmcli -t -f STATE,DEVICE device | awk -F: '$1=="connected" { print $2 }' } get_bytes() { cat "/sys/class/net/${1}/statistics/${2}_bytes" || echo 0 } update() { device=$1 cache=$2 rx_old=$(awk '$1=="rx" { print $2 }' "$cache" || echo 0) tx_old=$(awk '$1=="tx" { print $2 }' "$cache" || echo 0) rx_new=$(get_bytes "$device" rx) tx_new=$(get_bytes "$device" tx) printf "rx %s\n" "$rx_new" >"$cache" printf "tx %s\n" "$tx_new" >>"$cache" printf "rx=%s tx=%s" "$((rx_new - rx_old))" "$((tx_new - tx_old))" } rx_sum=0 tx_sum=0 for device in $(get_physical_devices); do cache=${CACHE}-${device} rx=0 tx=0 eval "$(update "$device" "$cache")" rx_sum=$((rx_sum + rx)) tx_sum=$((tx_sum + tx)) done printf " %5sB  %5sB" "$(numfmt --to=iec "$rx_sum")" "$(numfmt --to=iec "$tx_sum")"