0%

A Simple Shell Script to Measure Linux Network Traffic

This is a very simple script for measuring traffic on a local network interface under Linux. Before using it, adjust the configuration values and then run it.

There are two configuration options in the script:

  • eth_name=eth2: the network interface to inspect
  • wait_time=100: the sampling interval in seconds

Because the script itself also takes a little time to run, a longer wait_time usually gives a smaller error.

The full script is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh
# configure
eth_name=eth2
wait_time=100

# program
rxbyte_0=`ifconfig $eth_name | grep "RX bytes" | awk '{print $2}' \
| awk -F : '{print $2}'`
sleep $wait_time
rxbyte_1=`ifconfig $eth_name | grep "RX bytes" | awk '{print $2}' \
| awk -F : '{print $2}'`
rxbyte_result=`expr \( $rxbyte_1 - $rxbyte_0 \) / \( 131072 \* $wait_time \)`

name=`hostname`
printf "$name\t$rxbyte_result Mbps\n"
如果我的文字帮到了您,那么可不可以请我喝罐可乐?