Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pps.sh for habrahabr
#!/bin/bash
INTERVAL="1" # update interval in seconds
if [ -z "$1" ]; then
echo
echo usage: $0 [network-interface]
echo
echo e.g. $0 eth0
echo
echo shows packets-per-second
exit
fi
IF=$1
while true
do
R1=`cat /sys/class/net/$1/statistics/rx_packets`
T1=`cat /sys/class/net/$1/statistics/tx_packets`
sleep $INTERVAL
R2=`cat /sys/class/net/$1/statistics/rx_packets`
T2=`cat /sys/class/net/$1/statistics/tx_packets`
TXPPS=`expr $T2 - $T1`
RXPPS=`expr $R2 - $R1`
echo "TX $1: $TXPPS pkts/s RX $1: $RXPPS pkts/s"
done
@podlom
Copy link

podlom commented Jun 25, 2015

@pavel-odintsov
Copy link
Author

Fixed! thanks!

@evgenypim
Copy link

@novicevative
Copy link

Hi guys

If I use the script at https://gist.github.com/evgenypim/c5b3c39f73d885d3861f . Which I assumed will be correct because of the latest date
It throws out error
[user@server ~]# ./2pps.sh eth0
./2pps.sh: line 19: cat: No such file or directory
./2pps.sh: line 20: cat: No such file or directory
./2pps.sh: line 22: cat: No such file or directory
./2pps.sh: line 23: cat: No such file or directory
TX eth0: - pkts/s RX eth0: - pkts/s

after some testing I realized that syntax below had error
R1="$(<cat /sys/class/net/$IF/statistics/rx_packets)"

hence had to change it to
R1=$( cat /sys/class/net/eth0/statistics/rx_packets)

Moreover following also worked for me.
T1=cat /sys/class/net/$IF/statistics/tx_packets

Thanks

@mhristache
Copy link

A small improvement that makes the script work inside network namespaces:

#!/bin/bash

INTERVAL="1" # update interval in seconds

if [ -z "$1" ]; then
    echo
    echo usage: $0 [network-interface]
    echo
    echo e.g. $0 eth0
    echo
    echo shows packets-per-second
    exit
fi

IF=$1

SYS_PATH="/sys"
MNT=`mktemp -d`
mount -t sysfs none $MNT 2> /dev/null
if [ $? -eq 0 ]; then
    SYS_PATH="$MNT"
    trap 'umount $MNT' EXIT
fi

while true
do
    R1=`cat $SYS_PATH/class/net/$1/statistics/rx_packets`
    T1=`cat $SYS_PATH/class/net/$1/statistics/tx_packets`
    sleep $INTERVAL
    R2=`cat $SYS_PATH/class/net/$1/statistics/rx_packets`
    T2=`cat $SYS_PATH/class/net/$1/statistics/tx_packets`
    TXPPS=`expr $T2 - $T1`
    RXPPS=`expr $R2 - $R1`
    echo "TX $1: $TXPPS pkts/s RX $1: $RXPPS pkts/s"
done

@pavel-odintsov
Copy link
Author

Nice one! Thank you for sharing!

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