Ubuntu – How to get the current network traffic via the commandline in a simple format

command linenetworking

I need to calculate the average network traffic on an interface. I know about software like iftop, but I cannot use it in my case.

I am running a program which shall do calculations with the average network traffic on an interface. Therefore, I require a command which produces a simple output. Programs like iftop output the values in a very complex way which cannot be easily parsed by other applications.

I am looking for something like:

Command:

$ get-avg-traffic --interface=eth0 --seconds-interval=30 --type=incoming --unit=KB

Output:

783846

Is there already software which supports (some of) this functionality?

Best Answer

You probably want to have a look at vnstat

$ vnstat -d -i wlp3s0

wlp3s0  /  daily

     day         rx      |     tx      |    total    |   avg. rate
 ------------------------+-------------+-------------+---------------
 2017-10-24   166,63 MiB |   37,33 MiB |  203,96 MiB |   19,34 kbit/s
 2017-10-25   214,68 MiB |  130,26 MiB |  344,94 MiB |   32,71 kbit/s
 2017-10-26     1,01 GiB |    4,09 GiB |    5,10 GiB |  495,31 kbit/s
 2017-10-27     1,16 GiB |  113,28 MiB |    1,27 GiB |  123,04 kbit/s
 2017-10-28   201,09 MiB |  299,21 MiB |  500,31 MiB |   47,44 kbit/s
 2017-10-29     1,92 GiB |  174,35 MiB |    2,09 GiB |  202,92 kbit/s
 2017-10-30   559,56 MiB |   69,18 MiB |  628,73 MiB |   59,61 kbit/s
 2017-10-31   397,73 MiB |   43,62 MiB |  441,35 MiB |   41,85 kbit/s
 2017-11-01   665,81 MiB |   83,05 MiB |  748,86 MiB |   71,00 kbit/s
 2017-11-02   282,20 MiB |  239,91 MiB |  522,11 MiB |   49,50 kbit/s
 2017-11-03     4,06 GiB |    4,85 GiB |    8,92 GiB |  865,61 kbit/s
 2017-11-04   220,95 MiB |   40,12 MiB |  261,07 MiB |   24,75 kbit/s
 2017-11-05   320,91 MiB |    8,86 GiB |    9,18 GiB |  890,93 kbit/s
 2017-11-06   639,67 MiB |   13,77 GiB |   14,39 GiB |    1,40 Mbit/s
 2017-11-07   694,91 MiB |   80,48 MiB |  775,39 MiB |   73,52 kbit/s
 2017-11-08   178,64 MiB |   32,43 MiB |  211,07 MiB |   28,97 kbit/s
 ------------------------+-------------+-------------+---------------
 estimated       257 MiB |      46 MiB |     303 MiB |

You can have hourly daily and monthly statistics.

Related Question