How to get TX/RX bytes without ifconfig?
Solution 1:
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 2:
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 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