Search code examples
shellawkshifconfig

How to get Linux interface traffic details in bits instead of bytes?


I am trying to get the traffic details in a Linux box interface by running following:

/sbin/ifconfig eth0 |grep bytes|cut -d":" -f2|cut -d" " -f1

It's showing the result in bytes but i want the result in bits. I have tried with awk like this:

/sbin/ifconfig eth0 |grep bytes|cut -d":" -f2|cut -d" " -f1 | awk '{ SUM = $1*8; print SUM}'

but the result is showing like this: 1.488e+11

Can you please help me to modify the command; I need the result in full numbers, like: 18600143106.

Thank you.


Solution

  • Aside from changing the output format, when you're using awk you don't need to add a dozen other tools and pipes:

    /sbin/ifconfig eth0 | awk -F'[: ]' '/bytes/{sum = $2*8; printf "%d\n", sum}'
    

    Since you didn't post the output of ifconfig I'm just guessing from reading your script that $2 is the field you need. If not, just pick the right one.