How can I get an interface's bandwidth at any given second from the Linux command line?

3

I am doing a school project in which I want to get the bandwidth of a network interface at any given second, or some other small increment of time. I need this for a Perl script I am working on. Therefore it needs to be non-interactive and just prints results.

Any suggestions?

Alex

Posted 2010-11-06T19:25:41.987

Reputation: 245

Answers

3

The number of bytes sent and received by eth0 since the interface was brought up can be read in /sys/class/net/eth0/statistics/tx_bytes and /sys/class/net/eth0/statistics/rx_bytes respectively. The number of packets can be read in …/tx_packets and …/rx_packets. If you have an older kernel that doesn't provide these files, the data is available in the output of /sbin/ifconfig eth0.

Gilles 'SO- stop being evil'

Posted 2010-11-06T19:25:41.987

Reputation: 58 319

Exactly what I was looking for. – Alex – 2010-11-06T20:49:22.760

0

A simple approach would be:

  • execute ifconfig interface name once, capture its output
  • extract the values from "RX bytes" and "TX bytes"
  • wait for one second
  • repeat the first two steps again
  • compute the deltas

vtest

Posted 2010-11-06T19:25:41.987

Reputation: 4 424

Good answer too! Gilles' solution just happens to work perfect in my case. – Alex – 2010-11-06T20:49:55.850