ifconfig 中获取到的网卡流量信息可以用于统计服务器运行的流量,当有需要重置网卡流量 重新计数的时候,除了重启服务器,找了一圈有下面这个办法,停掉网卡之后卸载网卡模块,然后再加载启动网卡,相对于重启还是会好点。
-
获取网卡名称
-
重置网卡流量
-
一键脚本
可以自己写脚本去获取,但可能不会适应到所有的情况,而且比较费时,找了下用 lshw 可能会比较方便点,运行效果如下,运行的时候会有些信息闪动,最终还是可以获取到想要的信息。
[root@server ~]# sudo lshw -c network | grep "logical name" | awk '{print $3}' eth0
注意:可能需要 wc -l 判断一下网卡数量。
使用如下命令获取网卡模块信息:
[root@server ~]# ethtool -i eth0 | grep '^driver' | awk '{print $2}' virtio_net
重置网卡流量:
#停止网卡 ifconfig eth0 down #卸载模块 modprobe -r virtio_net #加载模块 modprobe virtio_net #启动网卡 ifconfig eth0 up
#!/bin/bash #init which lshw >/dev/null 2>&1 || yum install lshw -y >/dev/null which ethtool >/dev/null 2>&1 || yum install ethtool -y >/dev/null #get netconfig for netConfig in $(sudo lshw -c network | grep "logical name" | awk '{print $3}');do netModel=$(sudo ethtool -i ${netConfig} | grep '^driver' | awk '{print $2}') ifconfig ${netConfig} down modprobe -r ${netModel} modprobe ${netModel} ifconfig ${netConfig} up done
© 著作权归作者所有
下一篇: shell 获取邮件
文章评论(0)