Skip to content

Instantly share code, notes, and snippets.

@prochor666
Last active August 17, 2021 22:12
Show Gist options
  • Save prochor666/f5dc193a1fe01b9bef2c1e2cdbfa4e70 to your computer and use it in GitHub Desktop.
Save prochor666/f5dc193a1fe01b9bef2c1e2cdbfa4e70 to your computer and use it in GitHub Desktop.
Very basic netwrork statistics
#!/bin/bash
# @author prochor666@gmail.com
#
# Check stats for alive network devices
# RED 1, GREEN 2 and RESET color
DEVICEDOWN=$(tput setaf 1)
DEVICEUP=$(tput setaf 2)
RESET=$(tput sgr0)
for DEVICE in $(ls -I "lo" /sys/class/net);
do
STATEFILE="/sys/class/net/${DEVICE}/operstate"
if [[ -f ${STATEFILE} ]];
then
STATE="$(cat ${STATEFILE})"
if [[ "${STATE}" == "up" ]];
then
RXBYTESFILE="/sys/class/net/${DEVICE}/statistics/rx_bytes"
TXBYTESFILE="/sys/class/net/${DEVICE}/statistics/tx_bytes"
echo "Device ${DEVICE} is ${DEVICEUP}up${RESET}"
if [[ -f ${RXBYTESFILE} ]];
then
echo "RX (download): $(cat ${RXBYTESFILE})"
fi
if [[ -f ${TXBYTESFILE} ]];
then
echo "TX (upload): $(cat ${TXBYTESFILE})"
fi
else
echo "DEVICE ${DEVICE} is ${DEVICEDOWN}down${RESET}"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment