0

How can I get the in/out bytes separate for ifconfig??

I check packets and bytes by ifconfig .

ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 153.126.***.**  netmask 255.255.254.0  broadcast 153.126.179.255
    ether 9c:a3:ba:01:df:c3  txqueuelen 1000  (Ethernet)
    RX packets 60777328  bytes 18377900528 (18.3 GB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 33420428  bytes 11013732175 (11.0 GB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

I run this script every day and gets byte.

However in this way, in/out packets are mixed.

Is there a way to separate in/out packets??

Or is it impossible for ifconfig (I need to use another tools???)

What I want to do is just get the in/out bytes/packets per day.

PATH="/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin"

NIC="ens3"

LOG="/var/log/transmit_bytes.log"

AT=`date '+%Y-%m-%d %H:%M:%S'`

TX=`cat /proc/net/dev | grep $NIC | sed -e 's/:/ /' | awk '{print$10}'`

echo "${AT} ${TX}" >> $LOG

exit 0
1

You can use netstat -ni to get the packets in an easily-parsable table.

For example, netstat -ni

Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0      1500  1534430      0      0 0        605131      0      0      0 BMRU
lo       65536    20701      0      0 0         20701      0      0      0 LRU
tun0      1500   131763      0      0 0        177857      0      0      0 MOPRU
wlan0     1500        0      0      0 0             0      0      0      0 BMU

To pick out the received and transmitted packet count, something like this would be sufficient

netstat -ni | awk -v interface="eth0" '$1 == interface { print $3, $7 }'

Result

1534430 605131

For more modern systems with ip but without netstat, ip -s -j link show dev eth0 will provide a JSON formatted equivalent

[{"ifindex":2,"ifname":"eth0","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"pfifo_fast","operstate":"UP","linkmode":"DEFAULT","group":"default","txqlen":1000,"link_type":"ether","address":"b8:27:eb:31:53:64","broadcast":"ff:ff:ff:ff:ff:ff","stats64":{"rx":{"bytes":182767514,"packets":1538635,"errors":0,"dropped":0,"over_errors":0,"multicast":0},"tx":{"bytes":268406197,"packets":606995,"errors":0,"dropped":0,"carrier_errors":0,"collisions":0}}}]

This can be parsed with something like jq

ip -s -j link show dev eth0 | jq '.[].stats64 | ( .rx.packets, .tx.packets )' | xargs

Output

1538635 606995
1
  • Thank you very much!! I succeed to use netstat way. – whitebear Jun 3 '20 at 14:01

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.