#!/bin/sh # network activity [ ! -f /tmp/netactup ] || touch /tmp/netactup [ ! -f /tmp/netactdown ] || touch /tmp/netactdown # generate a list of devices net_devices=$(ls /sys/class/net | grep -E "[ew]") # get previous results rx_prev=$(cat /tmp/netactdown) tx_prev=$(cat /tmp/netactup) # convert results to integers if [ -n "$rx_prev" ] && [ -n "$tx_prev" ] ; then rx_prev=$(($rx_prev)) tx_prev=$(($tx_prev)) fi # declare send and recieve variables rx_now=0 tx_now=0 # convert variables to integers rx_now=$(($rx_now)) tx_now=$(($tx_now)) # iterate through each device for device in $net_devices; do rx_dev=$(cat /sys/class/net/"${device}"/statistics/rx_bytes) tx_dev=$(cat /sys/class/net/"${device}"/statistics/tx_bytes) rx_now=$(($rx_now + $rx_dev)) tx_now=$(($tx_now + $tx_dev)) done # caculate amount send and recieved down_total=$((rx_now - rx_prev)) up_total=$((tx_now - tx_prev)) echo $rx_now > /tmp/netactdown echo $tx_now > /tmp/netactup echo "D: $(numfmt --to=iec $down_total) U: $(numfmt --to=iec $up_total)" #echo "D:" $down_total "U:" $up_total