Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want the terminal to output the current upload and download speeds of my network connection. I do not want to monitor the speeds in real time. I just want a "static" value to be able to write it in a file.

How do I achieve it?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Oddly the easiest way seems to be looking at /proc/net/dev. I've written the following to compare that file twice (with a second delay) and then to subtract the total bytes values. In this case em1 is the network adaptor so just change that to whatever you need to look at.

awk '/em1/ {i++; rx[i]=$2; tx[i]=$10}; END{print rx[2]-rx[1] " " tx[2]-tx[1]}' \
 <(cat /proc/net/dev; sleep 1; cat /proc/net/dev)

The output is two numbers. Received bytes per second followed by sent bytes per second.

share|improve this answer
    
There we go. Boiled it down into one awk statement. Bootiful. –  Oli Apr 21 at 14:26

Here's a variation on Oli's excellent solution:

awk '{if(l1){print $2-l1,$10-l2} else{l1=$2; l2=$10;}}' \
  <(grep wlan0 /proc/net/dev) <(sleep 1; grep wlan0 /proc/net/dev)

It will print the same result as Oli's approach:

$ awk '{if(l1){print $2-l1,$10-l2} else{l1=$2; l2=$10;}}' \
>   <(grep wlan0 /proc/net/dev) <(sleep 1; grep wlan0 /proc/net/dev)
401500 30286

The first value is the download rate in bytes per second and the second is the upload rate. You could get a more human-friendly format with:

$ awk '{if(l1){print ($2-l1)/1024"kB/s",($10-l2)/1024"kB/s"} else{l1=$2; l2=$10;}}' \
    <(grep wlan0 /proc/net/dev) <(sleep 1; grep wlan0 /proc/net/dev)
398.771kB/s 82.8066kB/s
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.