sb-nettraf (893B)
1 #!/bin/sh 2 3 # Show per-second network traffic using consistent-width output 4 5 case $1 in 6 1) setsid -f "$TERMINAL" -e sudo -A bandwhich ;; 7 3) notify-send " Network traffic module" " : Traffic received 8 : Traffic transmitted" ;; 9 8) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;; 10 esac 11 12 update() { 13 sum=0 14 for arg; do 15 read -r i < "$arg" 16 sum=$(( sum + i )) 17 done 18 cache=/tmp/${1##*/} 19 [ -f "$cache" ] && read -r old < "$cache" || old=0 20 printf %d\\n "$sum" > "$cache" 21 printf %d\\n $(( sum - old )) 22 } 23 24 rx=$(update /sys/class/net/[ew]*/statistics/rx_bytes) 25 tx=$(update /sys/class/net/[ew]*/statistics/tx_bytes) 26 27 # Format bytes to IEC, strip spaces 28 rx_fmt=$(numfmt --to=iec --padding=4 $rx) 29 tx_fmt=$(numfmt --to=iec --padding=4 $tx) 30 31 # Ensure consistent width with %-6s (6-character padded fields) 32 printf " %-6s %-6s\n" "$rx_fmt" "$tx_fmt"