Skip to content

Instantly share code, notes, and snippets.

@stoyanovgeorge
Last active January 4, 2022 06:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoyanovgeorge/96e991fe23c1f3a036942daedc0e1afe to your computer and use it in GitHub Desktop.
Save stoyanovgeorge/96e991fe23c1f3a036942daedc0e1afe to your computer and use it in GitHub Desktop.
An one line command to get the average RX and TX speed of a particular interface (eth0) over $INT seconds in bytes per second. You need to divide by 128 to get it in Kbps or by 131072 (128*1024) for Mbps.
#!/bin/bash
INT=2; RXDIR=/sys/class/net/eth0/statistics/rx_bytes; TXDIR=/sys/class/net/eth0/statistics/tx_bytes; RXSTART=$(cat $RXDIR); TXSTART=$(cat $TXDIR); sleep $INT; RXEND=$(cat $RXDIR); TXEND=$(cat $TXDIR); RXBPS="$(((RXEND-RXSTART)/INT))"; TXBPS="$(((TXEND-TXSTART)/INT))"; echo "$RXBPS" "$TXBPS"
@stoyanovgeorge
Copy link
Author

stoyanovgeorge commented Apr 15, 2019

# This is the longer and more readable version of the upper one line command

#!/bin/bash

# Defining the interval
INT=2
RXDIR=/sys/class/net/eth0/statistics/rx_bytes
TXDIR=/sys/class/net/eth0/statistics/tx_bytes

# Saving the RX and TX bytes to $RXSTART and $TXSTART variables
RXSTART=$(cat $RXDIR)
TXSTART=$(cat $TXDIR)

# Waiting for $INT seconds
sleep $INT

# Saving the RX and TX bytes to $RXEND and $TXEND variables
RXEND=$(cat $RXDIR)
TXEND=$(cat $TXDIR)

# Calculation of the RX and TX speed in bytes per second
RXBPS="$(((RXEND-RXSTART)/INT))"
TXBPS="$(((TXEND-TXSTART)/INT))"

# Returning the results
echo "RX: $RXBPS bytes/s; TX: $TXBPS bytes/s"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment