如何获取自pc开机以来网络总使用量

分享于 

3分钟阅读

ubuntu

  繁體

问题:

输出:


wlp0s20f3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500


 inet 192.168.43.67 netmask 255.255.255.0 broadcast 192.168.43.255


 inet6 fe80::5f55:a45c:b66a:a7d2 prefixlen 64 scopeid 0x20<link>


 ether 48:f1:7f:f3:15:83 txqueuelen 1000 (Ethernet)


 RX packets 82724 bytes 108476705 (103.4 MiB)


 RX errors 0 dropped 0 overruns 0 frame 0


 TX packets 28959 bytes 3840172 (3.6 MiB)


 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0




答案1:

方法是使用Grep提取值。


# RX bytes


ifconfig wlp0s20f3 | grep -o '[0-9.]* MiB' | head -n1


# TX bytes


ifconfig wlp0s20f3 | grep -o '[0-9.]* MiB' | tail -n1



/sys/class/net/wlp0s20f3/statistics/ 目录。


cat /sys/class/net/wlp0s20f3/statistics/rx_bytes


cat /sys/class/net/wlp0s20f3/statistics/tx_bytes




答案2:

可以通过ip link命令获得stats (包括TX/RX字节和数据包计数):


ip -s -h link show



要限制输出到特定设备,请使用dev字段:


$ ip -h -s link show dev wls1


3: wls1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000


 link/ether 00:21:6a:96:40:12 brd ff:ff:ff:ff:ff:ff


 RX: bytes packets errors dropped overrun mcast


 4.52G 5.03M 0 1 0 0


 TX: bytes packets errors dropped carrier collsns


 357M 2.16M 0 0 0 0



这不是最容易解析的输出,可以使用类似


$ ip -s -h link show dev wls1 | 


 awk '/RX: bytes/{getline; printf "RX: %s, ", $1} /TX: bytes/{getline; printf "TX: %sn",$1}'


RX: 4.52G, TX: 357M



用你自己的接口名wls1替换wlp0s20f3


答案3

echo "Down: $(($(cat /sys/class/net/wlp0s20f3/statistics/rx_bytes)/1024/1024)) MiB, Up: $(($(cat /sys/class/net/wlp0s20f3/statistics/tx_bytes)/1024/1024)) MiB"



(wlp0s20f3)是我的设备。

结果:


Down: 103 MiB, Up: 4 MiB





COM  COMM  Comma  network  命令行  TERM  
相关文章