Simple traffic analyzer script

When you are working on embedded systems you couldn't have some useful tools to monitor the system resource;

In the following you will find a simple shell script I use when I want to monitor the network traffic on a Linux box.

This is a Spartan script but It does the work. It is also very simple to modify, for example you can add another parameter to select the period between two consecutive checks.

 

 

 

There is a known bug  , but I haven't time to correct it.

When, the value of the transmitted bytes will overcome the maximum value, you should see a negative value for the current bandwidth.

 

 

#!/bin/sh
# The NIC
interface=$1

# Compute the current received bytes
rx_byte_count() {
        grep $1 /proc/net/dev | awk '{print $2}'
}

# Compute the current transmitted bytes
tx_byte_count() {
        grep $1 /proc/net/dev | awk '{print $10}'
}

old_rx=$(rx_byte_count $interface)
old_tx=$(tx_byte_count $interface)
echo $old_rx
echo $old_tx

while grep $interface /proc/net/dev >/dev/null; do
        curr_rx=$(rx_byte_count $interface)
        curr_tx=$(tx_byte_count $interface)
        rx_diff=$((($curr_rx$old_rx)))
        tx_diff=$((($curr_tx$old_tx)))

        echo "$interface Rx=$rx_diff (B/s)  Tx=$tx_diff (B/s)"
        old_rx=$curr_rx
        old_tx=$curr_tx

        sleep 1
done

 

 

save this file as net.sh then run 

chmod +x net.sh

last run the monitor issuing the following command

./net.sh <eth device>

Gg1

You may also like...