0

I can see the download and upload speed for all interfaces using glances. In the network section we can see the Rx/s and Tx/s columns where I can see the value in Mbps.

However I want to store this value in a variable. Is there any other command, which gives the current speed (bps, or Kbps, or Mbps) so that I can save that value in a variable in a bash script ?

I want to do this for wlan0 interface.

1

There is not a single command to do this. However, you could use this:

eval $( \
    iwconfig wlan0 | \
    egrep -o 'Bit Rate=[0-9]+ [^ ]+' | \
    sed -e 's/ /_/;s/=/="/;s/$/"/' \
    )
echo $Bit_Rate

On my system, currently, I get:

$ eval $(iwconfig wlan0 | egrep -o 'Bit Rate=[0-9]+ [^ ]+' | sed -e 's/ /_/;s/=/="/;s/$/"/')
$ echo $Bit_Rate
54 Mb/s

iwconfig wlan0 outputs a bunch of information about wlan0 (try it and see).

Then egrep matches "Bit Rate=", followed by 1 or more digits, a blank, and 1 or more non-blanks. Because of the -o option, egrep outputs only the matched string.

sed then changes the first blank to an underscore, the '=' to '="', and the end-of-line to '"', producing a string like Bit_Rate="54 Mb/s".

eval then interprets that string in your current shell.

for further education, read

for i in iwconfig egrep sed bash ; do  
    man $i
done
  • thanks for the reply. however this value is always the same, and looks like it is the maximum rate. What I want to see is the actual current rate, which changes with time. Something like what can be seen when running glances . Please run glances , and see the network section. It has Rx, and Tx which reflect the actual rate at a particular time. It is that value I want to store in the variable. – sps Jan 6 '16 at 5:30
  • @sps I will not install glances to debug your problem. You can adapt my answer to handle the output from glances. Did you read the man pages I suggested? man iwconfig says, in part "All these parameters and statistics are device dependent. Each driver will provide only some of them depending on hardware support, and the range of values may change. Please refer to the man page of each device for details." – waltinator Jan 6 '16 at 15:12
  • Ya I am inside that for loop of man pages right now ... :) , .. – sps Jan 7 '16 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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