1

I do not need some kind of realtime visual status of my ethernet - I want to run my script when last five minutes I uploaded less than X. So I need to get only one number from some command. What can you recommend?

I use Ubuntu 14.04.

  • What operating system? – Celada Feb 21 '16 at 12:45
  • @Celada Ubuntu 14.04 – Vitaly Zdanevich Feb 21 '16 at 12:47
  • Please add that information to your question. It's very relevant. – Celada Feb 21 '16 at 12:54
  • @Celada added as tag – Vitaly Zdanevich Feb 21 '16 at 12:57
2

ifconfig <interface> gives you throughput of a specific interface.

For example,

root@trinity:~# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 28:92:4a:32:0c:43
          inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::2a92:4aff:fe32:c43/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1554100056 errors:0 dropped:3528 overruns:0 frame:15941
          TX packets:570492690 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2186365577866 (1.9 TiB)  TX bytes:180850207310 (168.4 GiB)
          Interrupt:18

Just read the TX bytes bit and do the maths. You'll need to track it in a file somewhere so you can work out the differential.

The ifconfig command is being deprecated, and people will suggest using ip. The relevant command with ip is,

root@trinity:~# ip -s link ls  eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT qlen 1000
    link/ether 28:92:4a:32:0c:43 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    2186366161514 1554101939 0       3197    15941   9994871
    TX: bytes  packets  errors  dropped carrier collsns
    180850392034 570493984 0       0       0       0
  • 1
    to get TX from ip: ip -s link ls eth1 | awk 'NR==6{print $1}' – Vitaly Zdanevich Feb 21 '16 at 13:18
5

Under Linux, reading /sys/class/net/<interface>/tx_bytes and /sys/class/net/<interface>/rx_bytes (depending on which direction you're interested in) is a good way to get the counters for the number of bytes received and the number of bytes sent on an interface. The counters are also available together for all interfaces in /proc/net/dev but you will have to do more parsing. You can read the values before you start transferring and read them again 5 minutes later then subtract to get the amount transferred during the 5 minute period.

  • 1
    /sys/class/net/*/statistics/tx_bytes is less deprecated and more convenient :). – sourcejedi Feb 21 '16 at 13:58
  • 1
    Very nice. I'll edit that in and use it in the future too. – Celada Feb 21 '16 at 13:59

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.