#!/bin/bash

IFACE_FILTER="${1:-}"
REPEATS="${2:-1}"
DELAY="${3:-1}"

snapshot() {
    local ts
    ts=$(date '+%Y-%m-%d %H:%M:%S')
    tail -n +3 /proc/net/dev | while read -r line; do
        name=$(echo "$line" | cut -d: -f1 | xargs)
        [[ -n "$IFACE_FILTER" && "$name" != "$IFACE_FILTER" ]] && continue

        rest=$(echo "$line" | cut -d: -f2)
        rx_packets=$(echo "$rest" | awk '{print $2}')
        tx_packets=$(echo "$rest" | awk '{print $10}')

        printf "%s  iface=%-10s RX=%-10s TX=%-10s\n" "$ts" "$name" "$rx_packets" "$tx_packets"
    done
}

for ((i = 1; i <= REPEATS; i++)); do
    snapshot
    (( i < REPEATS )) && sleep "$DELAY"
done
