Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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.

share|improve this answer
    
Exactly what I was looking for. – Alex Nov 6 '10 at 20:49

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
share|improve this answer
    
Good answer too! Gilles' solution just happens to work perfect in my case. – Alex Nov 6 '10 at 20:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .