0

I have tried to make script which can help me to output info about received and transmitted data on specific interface. Here is the beginning stuff of it:

#!/bin/bash

interface=$1

while true; do
ip -s link ls $interface | awk '{ print $1 "\t" $2}'

sleep 10
done

But also I want to get the difference of changes in data. I haven't the faintest idea how to output it. So for i.e. I get this from my script line ip -s link ls $interface | awk '{ print $1 "\t" $2}':

2:      enp0s3:
link/ether      08:00:27:ad:a6:53
RX:     bytes
38134   399
TX:     bytes
34722   247

I want to get the difference between 38134 and 34722 and then the difference between 399 and 247 and add to some file for example.

1

I have an ugly script that can answer your needs. The idea is:

  • store interface stats in a file
  • read the file line by line
  • if line containts RX (resp TX), that means that the following line contains information you want to parse.

The script:

#!/bin/bash

ip -s link ls $interface > ip_stats

RX=0
TX=0
# read file
while read LINE
do 
    # read RX info form line
    if [ $RX -eq 1 ]
    then
        RX_packets=$(echo $LINE | awk '{print $1}')
        RX_bytes=$(echo $LINE | awk '{print $2}')        
    fi
    # see if next line will contain RX stats
    if echo $LINE | grep RX 
    then
        RX=1
    else
        RX=0
    fi

    # read TX info form line
    if [ $TX -eq 1 ]
    then
        TX_packets=$(echo $LINE | awk '{print $1}')
        TX_bytes=$(echo $LINE | awk '{print $2}')        
    fi
    # see if next line will contain TX stats
    if echo $LINE | grep TX 
    then
        TX=1
    else
        TX=0
    fi

done < ip_stats

echo RX_packets is $RX_packets
echo TX_packets is $TX_packets

echo RX_bytes is $RX_bytes
echo TX_bytes is $TX_bytes

# make diff
echo packets diff: $(expr $RX_packets - $TX_packets )
echo bytes diff: $(expr $RX_bytes - $TX_bytes )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.