- 
      
- 
        Save pavel-odintsov/bc287860335e872db9a5 to your computer and use it in GitHub Desktop. 
| #!/bin/bash | |
| # Interval of calculation in seconds | |
| INTERVAL="1" | |
| 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 -e "TX $TXPPS\tpkts/s RX $RXPPS\tpkts/s" | |
| done | 
Prettify Bash https://gist.github.com/evgenypim/c5b3c39f73d885d3861f
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
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"
doneNice one! Thank you for sharing!
Fixed! thanks!