linux shell获取网口收发流量

先上指令:

获取接收数据总字节数:

ifconfig eth0 | grep 'byte' | sed 's/^.*RX[[:space:]]bytes://' | sed 's/[[:space:]].*$//'

获取发送数据总字节数:

ifconfig eth0 | grep 'byte' | sed 's/^.*TX[[:space:]]bytes://' | sed 's/[[:space:]].*$//'

解释:

通过grep 可以过滤出只包含 byte 字符的行

linux shell获取网口收发流量

获取出 收发数据量的行后,需要删除 多余的字符串。这里用道sed命令

sed 可以用来替换 我们需要删除的字符为 空字符。

sed 's/^.*RX[[:space:]]bytes://' 的含义是替换从行头开始,到'RX bytes:' 结尾的字符为空。

linux shell获取网口收发流量

前面的字符删掉了,后面的字符也需要删掉。

sed 's/[[:space:]].*$// 含义是替换从空格开始到行尾所有的字符为空。

linux shell获取网口收发流量

使用示例:

linux shell获取网口收发流量