How to get TX/RX bytes without ifconfig?

101,024

Solution 1

Another option is to use the /proc filesystem. The /proc/net/dev file contains statistics about the configured network interfaces. Each line is dedicated to one network interface and it contains statistics for receive and transmit. The statistics include metrics such total number of received/transmittted bytes, packets, drops, errors and so on.

cat /proc/net/dev

    Inter-|   Receive                                                |  Transmit
     face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed

    lo:    29846937   129576     0    0    0     0          0       0 29846937   129576     0    0    0     0       0          0
 wlan0:    9467393340 8027251    0    0    0     0          0       0 2559312961 5896509    0    0    0     0       0          0

Or you can try the netstat command which can display all network interfaces and related statistics:

netstat -i

Iface   MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
lo         65536   0   130435      0      0      0   130435      0      0      0 LRU
wlan0       1492   0  8028018      0      0      0  5897361      0      0      0 BMRU

Solution 2

The ip command which is part of the the iproute2 package is the new tool. The link subcommand is for managing the devices/interfaces.

If you can get the stats of an interface using ip -s link

root:~# ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX: bytes  packets  errors  dropped overrun mcast
    50679705   529967   0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    50679705   529967   0       0       0       0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:1d:7d:aa:e3:4e brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    187663757  308710386 0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    4051284587 532435117 0       0       0       0

Solution 3

You can get all necessary info via proc

# cat /sys/class/net/eth0/statistics/rx_bytes
# cat /sys/class/net/eth0/statistics/rx_packets

# cat /sys/class/net/eth0/statistics/tx_packets
# cat /sys/class/net/eth0/statistics/tx_bytes

Also you can use iptables and parse output.

For received packets

# iptables -L INPUT -n -v

for transmitted packets

# iptables -L OUTPUT -n -v 

If server is a gateway, then you should also parse FORWARD chain

Solution 4

You can read the file /sys/class/net/wlp3s0/statistics/rx_bytes and get the rx_byes directly without calling another command, vnstat is also good. Linux stores all information in files as I know, so better to find those files and get information. Finding the relevant file is the challenge.

Share:
101,024

Related videos on Youtube

justinas
Author by

justinas

Updated on September 18, 2022

Comments

  • justinas
    justinas almost 2 years

    Since ifconfig is apparently being deprecated in major Linux distributions, I thought I'd learn something about the ip tool that's supposed to be used instead of ifconfig.

    And here I ran into a problem: when run on its own, ifconfig shows the number of bytes received/transmitted on each interface besides other info. I couldn't find a way to get this from ip. Is there no such function in this tool? What other built-in tools could I use for getting those stats?

  • justinas
    justinas almost 11 years
    /proc/net/dev has the cleanest format of all of the solutions IMO, accepting.
  • Moshe Katz
    Moshe Katz almost 11 years
    While the proc answer is clean and lightweight, this answer is probably the closest to a "replacement" for the ifconfig tool because it was explicitly designed with replacement of ifconfig in mind.
  • lalebarde
    lalebarde almost 10 years
    Stats showed by cat /proc/net/dev are for how long time ?
  • Piotr Kula
    Piotr Kula about 9 years
    How do you get tx/rx from /proc/net/dev ?
  • dsmsk80
    dsmsk80 about 9 years
    Try to parse 3. and 11. field of the output, something like awk '/:/ { print($1,$3, $11) }' < /proc/net/dev
  • Admin
    Admin over 7 years
    Welcome to the community! Well done, that's correct, but note that some old kernels don't provide these files. Wish you luck, and success! ;)
  • pim
    pim about 6 years
    ip -s link show dev eth0 to show only traffic on eth0
  • Joseph Redfern
    Joseph Redfern about 5 years
    To get this in human readable form (reporting in mb/gb/tb rather than bytes), use the -h flag. For instance, ip -h -s link.
  • Mike S
    Mike S about 3 years
    netstat is deprecated along with ifconfig. On Linux, not sure about other *IX varietals.
  • Admin
    Admin about 2 years
    netstat -i cuts off interface names after the 8th char, not very usable if your system has longer interface names (which mine does).